Subject: Why won't this code work? · Posted: 2005-01-19, 02:43pm
I am pretty new to assembly. I am trying to write some real mode bootsector code. I am using NASM.
I seem to be having trouble understanding how NASM or x86 real mode handle segments and offsets. For the moment, I am mixing my code and data into one plain binary file.
To my mind, the following code should work:
[BITS 16] ; real mode is 16 bit
mov ax, cs ; copy code segment (CS) to AX
mov ds, ax ; copy CS to data segment (DS)
mov al, [message] ; load AL with first byte of message
mov ah, 0x0E ; print byte in AL using BIOS interrupt 10h:0Eh
mov bx, 0x07 ; set character attributes (white on black)
int 0x10 ; call BIOS interrupt
db 'Hello World', 0x00
;plus code to fill up to 512 bytes with 0x55AA at the end - works fine, not important here
When I compile and run this code, my Vmware BIOS prints a single 'S'. I haven't tried it on other BIOS's yet. This is not the intended result. Shouldn't it print an 'H'?
I am aware of an easy workaround. I can just tell NASM to start at 0x07C00 (instead of default 0x00?) by adding a [ORG 0x07C00] to the start, and then setting DS to 0x00, rather than setting it to CS. That's fine, and that's what I do for the moment, but Id still like to know why the above code doesn't work - what am I missing?
When I make a memory reference such as [message], presumably this is just a 'displacement' reference using DS as the segment?