Data labels don't work with ds

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
alethiophile
Member
Member
Posts: 90
Joined: Sat May 30, 2009 10:28 am

Data labels don't work with ds

Post by alethiophile »

I have written code to print a string using int 0x10. It uses lodsb to get a byte at ds:si. In my initialization code, I set all the segment registers, as follows:

Code: Select all

	cli
	mov     ax,     0x07c0
	mov     ds,     ax      ;; for some reason, this breaks printing the message
        mov     es,     ax
        mov     fs,     ax
	mov     gs,     ax

        mov     ax,     0x0000
	mov     ss,     ax
        mov     sp,     0xFFFF
        sti
(This is in a stage-1 bootloader, hence the 0x07c0.) The printing code:

Code: Select all

;; Method: print
;;--------------------------------------
;; ds:si => string to print
;;--------------------------------------
;; Prints a null-terminated string using BIOS interrupt 0x10.
print:
        lodsb
        or      al,     al
        jz      print_end
        mov     ah,     0x0e
        int     0x10
        jmp     print
print_end:
        ret
It uses lodsb, hence needs the data byte to be at ds:si. However, when I call the print routine, as follows:

Code: Select all

        mov     si,     loadmsg
        call    print
it doesn't work. If ds is zeroed (instead of set to 0x07c0), it works fine. Reading this in the bochs debugger, it appears that loadmsg is being replaced by its full address (0x7c3f) rather than just 0x3f, as should be to print properly with lodsb. Confirming this, if I replace the call code above with

Code: Select all

        lea     si,    [loadmsg - 0x7c00]
        call    print
it works. At the start of my bootsector, I use

Code: Select all

org 0x7c00
This seems as if it should work. Am I doing something wrong? Why is this being parsed this way?
If I had an OS, there would be a link here.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Data labels don't work with ds

Post by neon »

If you are using org 0x7c00, then the segment registers should be 0, else you will have odd offset problems.

Either of the following two should work:

Code: Select all

org 0x7c00
xor ax, ax
mov ds, ax

Code: Select all

org 0x0
xor ax, 0x7c0
mov ds, ax
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
alethiophile
Member
Member
Posts: 90
Joined: Sat May 30, 2009 10:28 am

Re: Data labels don't work with ds

Post by alethiophile »

Thanks, that fixed it.
If I had an OS, there would be a link here.
Post Reply