Assembly (x86): <label> db 'string',0 does not get executed unless there's a jump instruction
Asked Answered
S

2

3

I've been banging my head against the wall in an attempt to understand why the following assembly is not correctly dumping the contents of 'HELLO_WORLD'.

; Explicitly set 16-bit
[ BITS 16 ]
[ ORG 0x7C00 ]

; Create label for hello world string terminated by null.
HELLO_WORLD db 'hello world', 0

start:
    ; Move address of HELLO_WORLD into si
    mov SI, HELLO_WORLD
    call print_string

    ; Continue until the end of time
    jmp $

print_string:
    loop:
        ; Retrieve value stored in address at si
        mov al, [SI]
        mov ah, 0x0E
        cmp al, 0
        ; Finish execution after hitting null terminator
        je  return
        INT 0x10
        ; Increment contents of si (address)
        inc SI
        jmp loop

    return:
        ret

; boot loader length *must* be 512 bytes.
times 510-($-$$) db 0
dw 0xAA55

In the end, I discovered that if we do not execute (make it not code) the label, then it functions correctly.

jmp start
HELLO_WORLD db 'hello world',0    

The part I find the most confusing, looking at the hex dump, HELLO_WORLD is still in the binary (at the beginning - and there appears to be no distinction of its type).

cat nojmp_boot.out

00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 00 be 00 7c e8  |hello world...|.|
00000010  02 00 eb fe 8a 04 b4 0e  3c 00 74 05 cd 10 46 eb  |........<.t...F.|
00000020  f3 c3 eb e8 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

cat jmpboot.out

00000000  eb 22 68 65 6c 6c 6f 20  77 6f 72 6c 64 00 be 02  |."hello world...|
00000010  7c e8 02 00 eb fe 8a 04  b4 0e 3c 00 74 05 cd 10  ||.........<.t...|
00000020  46 eb f3 c3 eb e8 00 00  00 00 00 00 00 00 00 00  |F...............|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200

Inspecting the first two bytes, we can see 'e8 22' is a shortjump to address 22 (http://net.cs.uni-bonn.de/fileadmin/user_upload/plohmann/x86_opcode_structure_and_instruction_overview.pdf).

My question is:

Why can we not have 'HELLO_WORLD' as a part of the execution of the program, as far I was concerned, there was no distinction between code and data?

I'm using the following for compilation:

nasm -f bin -o boot.bin boot.asm && if [ $(stat -c "%s" boot.bin) -ne 512 ]; then x; fi && qemu-system-x86_64 boot.bin
Sarcous answered 31/5, 2015 at 18:53 Comment(1)
Related Segmentation fault when using DB (define byte) inside a function same bug in user-space under an OS in protected mode, instead of bootloader, so the symptoms are slightly different. (And with details on how the common 00 00 byte sequence decodes as a memory-destination add).Goldenrod
D
5

Execution starts at the top. If you omit the jmp start then the character h will get interpreted by the CPU as if it were an instruction. Surely you see that such can not be correct?

as far I was concerned, there was no distinction between code and data?

There's no distinction between code and data when we consider their placement in the binary. But code and data still remain 2 completly different items. Code being the only one that can get executed by the CPU.

Damiano answered 31/5, 2015 at 19:0 Comment(4)
Many thanks user3144770. Out of curiosity, is there any reason I'm not able to take the address of HELLO_WORLD (as it is a label) and place it in a register, later to be iterated? (I'm thinking byte stream)Sarcous
mov si, HELLO_WORLD already does what you ask. (NASM syntax)Damiano
The problem is, if I then try to use: mov al, [SI] It terminates at cmp al, 0 je returnSarcous
I don't see a problem with that. This is exactly what you need in this bootloader program that you showed us. The text "hello world" has to end sometime and that's done here by appending a zero-byte and checking for it.Damiano
C
3

Since you're creating a boot sector the execution begins at the first byte of the generated file. It won't begin at the start label or anywhere else. Since the string "hello world" is at the start of the file these bytes are what get executed first. These bytes are interpreted by the CPU as instructions, not characters, and they get executed as whatever instructions they get decoded as.

Here are the instructions that get executed:

7c00:   68 65 6c                push   0x6c65
7c03:   6c                      ins    BYTE PTR es:[di],dx
7c04:   6f                      outs   dx,WORD PTR ds:[si]
7c05:   20 77 6f                and    BYTE PTR [bx+0x6f],dh
7c08:   72 6c                   jb     0x7c76
7c0a:   64 00 be 00 7c          add    BYTE PTR fs:[bp+0x7c00],bh
7c0f:   e8 02 00                call   0x7c14
7c12:   eb fe                   jmp    0x7c12
7c14:   8a 04                   mov    al,BYTE PTR [si]
...
Chungchungking answered 31/5, 2015 at 19:1 Comment(1)
Thanks - definitely makes sense. Asked the following question above as well: Out of curiosity, is there any reason I'm not able to take the address of HELLO_WORLD (as it is a label) and place it in a register, later to be iterated? (I'm thinking byte stream)Sarcous

© 2022 - 2024 — McMap. All rights reserved.