stack problem i guess

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
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

stack problem i guess

Post by nitinjavakid »

mov ax, 0x07c0
mov ds, ax
mov es, ax
mov esp, 0x9c00
mov dh,0
mov dl,0
mov bl,0
push DWORD msg
startagain:

push bx
mov ebx, esp
inc ebx
inc ebx
mov bp, [ebx]
pop bx
mov ah,0x13
mov al,0x01
mov bh,0
mov cx, 21
int 0x10
inc dh
inc dl
inc bl
hang:
jmp startagain

msg db 'Welcome to Macintosh', 13, 10, 0


I am unable to print the text in msg using push. However was able to do so by directly assigning address of msg to bp. Can you plz tell me what can be the possible mistake?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Several things i picked up:

These two probably form your problem:
- SS isnt being initialized (it can be pointing anywhere)
- if you push it'll be sent to the stack segment: SS:SP, however you attempt to read it back from the data segment: DS:eBX. if you attempt to access a memory location using ebp or esp, it'll go to the stack segment by default, if you dont, it'll go to the data segment. If you dont like that, add a DS or SS prefix. (like mov BP, [SS:eBX])

These are probably not good either
- i dont see an org 0x7C00, nor a far jump to 7C0:0, which means that data, offsets and such may be way off
- the usage of 32-bits registers is kindof... strange. Especially since you're only pushing the first 16 bits and not all 32 of them.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
smiddy
Member
Member
Posts: 127
Joined: Sun Oct 24, 2004 11:00 pm
Location: In my cube, like a good leming. ;-)

Post by smiddy »

I agree with Combuster, you'll want to initialize SS = CS = ES and I would use SP and BX instead of ESP and EBX. Depending on your assembler the default ORG may not be right. From what I see here you're assuming ORG 0.

I haven't seen anyone use INT 10h AH=13h before, usually most folks use INT 10h AH=0Eh.

Code: Select all

        mov si, msg

PrintString:

        mov ah, 0eh             ; Teletype output
        xor bx, bx              ; Page 0, color 0 (graphic only)

.Loop:

        lodsb                   ; Load AL with [SI] increment SI by 1
        or al, al               ; Used to test for 0 terminated string
        jz $                    ; Stops here when done (add another label and put a RET afterwards)
        int 10h
        jmp short .Loop
Also, from http://my.execpc.com/CE/AC/geezer/osd/boot/index.htm:

Your boot code should initialize the following registers:
  • DS. Some BIOSes set this register to 0, some set it to 40h. It should be set to (7C00h - BOOT_ORG) / 16. BOOT_ORG is the ORG value of your boot code, usually 7C00h.
    SS and SP (the stack). The initial values in these registers depend on the BIOS
    CS and IP (re-initialize these with a far JMP). Most BIOSes enter the boot code at address 0000:7C00h, but some (like the Compaq Presario 4328) jump to 07C0:0000h. Because short and conditional jumps are IP-relative, there is no need to reload CS and IP if your boot code does not use far or absolute JMPs. DS must still contain the correct value, however.
Hope this helps...
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Thanks for the reply, here is a bit detailed and rectified(i hope so) code. It shows a wierd character on the screen.
I am using nasm

Code: Select all

   mov ax, 0x07c0
   mov ds, ax
   mov es, ax ; used by int 0x10 13h

   ; stack thing i am unsure if it is right.
   mov ax, 0x00
   mov ss, ax    
   mov sp, 0x9c00 

   push DWORD msg  ; pushing the address of msg
startagain:

   ;setting the address for bp(accessed by int 0x10 13h   
   push bx
   mov bx, sp
   inc bx
   inc bx
   mov bp, [bx]
   pop bx

   
   mov ah,0x13
   mov al,0x01
   mov bl,0x06  ; rgb 110 color
   mov dh,1   ; row
   mov dl,1   ; column
   mov bh,0   ; page
   mov cx, 21 ; length
   int 0x10
hang:
   jmp startagain

msg   db 'Welcome to Macintosh', 13, 10, 0
The above example works fine when I pass address of msg directly to bp so that int 0x10 13h can use ES:BP for printing the string. I wanted to try it by passing the address of msg through stack and as you can see I am unable to do so :(.
hmmm interesting......

Code: Select all

   mov ax, 0x07c0
   mov ds, ax
   mov es, ax ; used by int 0x10 13h

   ; stack thing i am unsure if it is right.
   mov ax, 0x00
   mov ss, ax    
   mov sp, 0x9c00 

   push DWORD msg  ; pushing the address of msg
startagain:

   ;setting the address for bp(accessed by int 0x10 13h   
   push 2 ; pushing something stupid into stack
   mov bp, sp
   mov bp, [bp+2]

   pop ax ; poping something stupid
   mov ah,0x13
   mov al,0x01
   mov bl,0x06  ; rgb 110 color
   mov dh,1   ; row
   mov dl,1   ; column
   mov bh,0   ; page
   mov cx, 21 ; length
   int 0x10
hang:
   jmp startagain

msg   db 'Welcome to Macintosh', 13, 10, 0
this works. but still, is this luck or i am doing it the right way? :(([/code]

also this doesnt work.
mov bx,sp
mov bp, [bx+2]
Is there any reason for the above?
Thanks again for reading.
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Both bp and bx are 32 bit registers right?
If yes, then why cant bx be used instead of bp?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

BX and BP are 16-bit
eBX and eBP are 32-bit

the probable cause for your bug still exists: i see no SS being set, and you are still reading from an nonexistant stack in DS instead of SS:

some x86 logic:
mov ax, [bx], will read from DS:BX
mov bp, [bx], will also read from DS:BX, as the memory operand doesnt contain (e)BP or (e)SP
mov ax, [bp]. will read from SS:BP
mov ax, [SS:bx], will read from SS:BX

pushes will go to SS, so whatever you push to the stack, you need to read back from SS, NOT DS

mov bp, [SS:bx] will probably solve your problem
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
nitinjavakid
Member
Member
Posts: 65
Joined: Sat Oct 21, 2006 11:28 am
Location: Exams over!
Contact:

Post by nitinjavakid »

Thanks dude! Great help!
Post Reply