I have started understanding assembly language. I tried to understand the memory layout and addressing of variables in data section and wrote the following code
mov bx,char1 ;copies the address to register bx
mov ah,0Eh
mov al,bh ;moves the higher bit to al
add al,65 ;added 65 to al to see a recognizable asci character on screen
int 10h ;print statement 1
mov al,bl ;moves the lower bit to al
add al,50
int 10h ;**print statement 2**
;**section 2 start**
mov bx,char2
;**section 2 end**
jmp $
char1: db "X"
char2: db "Y"
times 510 - ($-$$) db 0
dw 0xaa55
As far I understand, char1 points to a byte in memory and char2 points to the very next byte. But I don't know why the presence of section 2 is affecting the character printed by print statement 2. I have tested this in QEMU in a 64-bit platform. Can somebody please help me overcome this?
Thank you in advance
addressing of variables in data section
I believe your confusion stems from this idea that your variables are in a separate 'data' section.
Many assemblers will allow you to organize the program in multiple sections like .stack
, .data
, and .code
, and if you do that kind of programming, then the offset address of a data item would not change after inserting an extra instruction.
But your current bootsector code is much simpler. You are not using sections at all. Everything you write gets encoded right where it is.
The code that prints the address occupies 17 bytes.
In the abscense of the 'section 2 instruction', the address of the char1 variable would be 19. That's 17 plus the 2 bytes comming from the jmp $
instruction.
By inserting the 'section 2 instruction', the address of the char1 variable became 22. That's 17 plus the 3 bytes coming from mov bx, char2
plus the 2 bytes coming from the jmp $
instruction.
ps I'm assuming nothing comes before the printing code...