The position of the label declaration affecting the exit

I wrote a build program to print a string:

[org 0x7c00]

mov bx, HELLO_MSG

HELLO_MSG:
db "Hello World!", 0

mov ah, 0x0e

PRINT:
mov al, [bx]
cmp al, 0
je END
int 0x10
add bx, 0x1
jmp PRINT

END:

jmp $
times 510-($-$$) db 0
dw 0xaa55

      

when compiled with nasm, it generated the following binary

BB 12 7C B4 0E 8A 07 3C 00 74 07 CD 10 83 C3 01 EB F3 48 65 6C 6C 6C 6F 20 57 6F 72 6C 64 21 00 EB FE 00 00 .... 00 00 55 AA

      

Quit using qemu emulator

since it is clear that

since it is clear that "ll" is replaced by other characters.

However, if I move the label HELLO_MSG

to the bottom of the code just above jmp $

, the output is correct. I cannot understand the reason for this.

EDIT: I noticed the following outputs when trying to use different strings instead of "Hello World" in the source code

Case: "Hellllo World" (note the "l")

trash letters only show up on those two bytes

trash letters only show up on those two bytes

Case: "We are gods"

strange error is gone!

strange error is gone!

Case: "We are gods!" (pay attention to "!" )

nothing is printed by adding '!'  something terrible?

nothing is printed by adding '!' something terrible?

Case: "Hello World" (note! '!')

remove '!'  did something terrible again?

removing '!' did something terrible again?

+3


source to share


1 answer


You put a line in the middle of the executable code. So ASCII values ​​are treated as command opcodes and perform some operation that appears to be happening to overwrite some bytes.



You must put the line at the end after the statement jmp

so that it won't be executed. Alternatively, you can add a statement jmp

before jumping the line.

+5


source







All Articles