Why is the register in x86-assembly (r) si moving to itself if I try to point it to a label?

I want to write a bootloader that just prints "Hello World!". on the screen and I don’t know why my bytes are messed up. I am trying to write it in AT & T syntax (please do not recommend Intel syntax) and try to convert the code from this lesson to AT & T syntax.

Now here is a pretty short code for my bootloader:

start:
.code16         #real mode
.text
.org 0x0
.globl _main
_main:
    movw hello, %si
    movb $0x0e, %ah

loophere:
    lodsb
    or %al, %al     #is al==0 ?
    jz halt         #if previous instruction sets zero flag jump to halt
    int $0x10       #run bios interrupt 0x10 (ah is set to 0x0e so a character is displayed)
    jmp loophere


halt:
    cli
    hlt


hello:  .ascii "Hello world!\0"


filloop:    
    .fill (510-(.-_main)),1,0   #I hope this works. Fill bootloader with 0 until byte 510


end:
    .word 0xaa55

Now, if I compile this with

$as -o boot.o boot.as
$ld -Ttext 0x07c00 -o boot.elf boot.o
$objcopy -O binary boot.elf boot.bin

next command

$objdump -d boot.elf

gives me this showdown

Disassembly of section .text:

0000000000007c00 <_main>:
    7c00:   8b 36                   mov    (%rsi),%esi
    7c02:   11 7c b4 0e             adc    %edi,0xe(%rsp,%rsi,4)

0000000000007c06 <loophere>:
    7c06:   ac                      lods   %ds:(%rsi),%al
    7c07:   08 c0                   or     %al,%al
    7c09:   74 04                   je     7c0f <halt>
    7c0b:   cd 10                   int    $0x10
    7c0d:   eb f7                   jmp    7c06 <loophere>

0000000000007c0f <halt>:
    7c0f:   fa                      cli    
    7c10:   f4                      hlt    

0000000000007c11 <hello>:
    7c11:   48                      rex.W
    7c12:   65 6c                   gs insb (%dx),%es:(%rdi)
    7c14:   6c                      insb   (%dx),%es:(%rdi)
    7c15:   6f                      outsl  %ds:(%rsi),(%dx)
    7c16:   20 77 6f                and    %dh,0x6f(%rdi)
    7c19:   72 6c                   jb     7c87 <filloop+0x69>
    7c1b:   64 21 00                and    %eax,%fs:(%rax)

0000000000007c1e <filloop>:
    ...

0000000000007dfe <end>:
    7dfe:   55                      push   %rbp
    7dff:   aa                      stos   %al,%es:(%rdi)

if i hexdump it (you can also see bytes in the parsing above) my first 6 bytes

8b 36
11 7c b4 0e

be 10 7c b4 0e ( ). , ac lodsb (loadstringbyte), b4 0e 0e %ah, be 10 7c %si hello 7c10 ( ). , . , :

0000000000007c00 <_main>:
    7c00:   be 10 7c b4 0e          mov    $0xeb47c10,%esi
    7c05:   ac                      lods   %ds:(%rsi),%al

"S". - , -?

64- Debian 9 qemu-system-x86_64 .

+4
1

16-, OBJDUMP -Mi8086. 64- AS LD, 64- . -M . i8086 - 16- .

, DS. . AT & T $ , ( ). movw hello, %si movw $hello, %si. LEA, ( ). $. leaw hello, %si .

INT 10h/AH = 0Eh BH, . 0 - .

, :

start:
.code16         #real mode
.text
.globl _main
_main:
    xor  %ax, %ax      # We  are usin offset 0x7c00, thus we need to se segment to 0x0000
    mov  %ax, %ds
    mov  %ax, %es
    mov  %ax, %ss      # Set the stack to grow down just below bootloader
    mov  $0x7c00, %sp
    cld                # Ensure forward movement of lods/movs/scas instructions

    movw $hello, %si   # We want the address of hello, not what it points at
    #leaw hello, %si   # Alternative way to get address with LEA instruction.
    movb $0x0e, %ah
    xor  %bh, %bh      # Make sure video page number is set (we want 0)

loophere:
    lodsb
    or %al, %al     #is al==0 ?
    jz halt         #if previous instruction sets zero flag jump to halt
    int $0x10       #run bios interrupt 0x10 (ah is set to 0x0e so a character is displayed)
    jmp loophere


halt:
    cli
    hlt


hello:  .ascii "Hello world!\0"


filloop:
    .fill (510-(.-_main)),1,0   #I hope this works. Fill bootloader with 0 until byte 510


end:
    .word 0xaa55
+6

Source: https://habr.com/ru/post/1688222/


All Articles