Stack question

Programming, for all ages and all languages.
Post Reply
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Stack question

Post by matias_beretta »

Hello, thanks for reading my topic. I've a question about stack:

Code: Select all

org 7c00h

mov ax, 0h
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 200h
This code sets up a stack next to the bootsectors' 512 bytes, so stack will grow downwards 7c0h:200h?????

Sorry for my english... :( I've been learning english for 7 years...
Matías Beretta
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

No.

The ORG command only effects absolute references in the code where it needs to work out where in memory the label referenced will be.

For example

Code: Select all

ORG 0x7C00
JMP FAR CS:MyLabel
MyLabel:JMP MyLabel
I apologise if the first JMP isn't correct Syntax, i'm just using it as an example. NASM would assemble the code to "JMP CS:0x7c04" or there about's. As you can see, the ORG directive has absolutely no effect at runtime and is simply a base for assembling.

The Stack's Base is determined by the SS (Stack Segment) Register. You are fixing this at 0x0000 with the combination.

Code: Select all

MOV AX, 0x0000
MOV SS, AX
Therefore your stack before any pushes points to 0x0000:0x0200, a bad idea considering it is in the middle of the IVT and will soon begin overwriting active IVT entries.
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Reply

Post by matias_beretta »

Code: Select all

org 7c00h

mov ax, 7c0h
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 200h
So this is the correct code?
Matías Beretta
User avatar
matias_beretta
Member
Member
Posts: 101
Joined: Mon Feb 26, 2007 3:39 pm

Post by matias_beretta »

Example without org:

Code: Select all

; boot.asm
   mov ax, 0x07c0
   mov ds, ax

   mov si, msg
ch_loop:lodsb
   or al, al ; zero=end or str
   jz hang   ; get out
   mov ah, 0x0E
   int 0x10
   jmp ch_loop

hang:
   jmp hang

msg   db 'Welcome to Macintosh', 13, 10, 0
   times 510-($-$$) db 0
   db 0x55
   db 0xAA
Example with org:

Code: Select all

[ORG 0x7c00]

   xor ax, ax ; make it zero -----------> i think what i'm doing is ok
   mov ds, ax

   mov si, msg
ch_loop:lodsb
   or al, al  ; zero=end of string
   jz hang    ; get out
   mov ah, 0x0E
   int 0x10
   jmp ch_loop

hang:
   jmp hang

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

   times 510-($-$$) db 0
   db 0x55
   db 0xAA
Matías Beretta
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

The example code's aren't equivelent. As i said, ORG has no effect on the runtime, only on how the assembler computes the absolute location of labels within your source.

Code: Select all

org 7c00h 

mov ax, 7c0h 
mov ds, ax 
mov es, ax 
mov ss, ax 
mov sp, 200h 
This code will create a stack at 0x7C00:0x200 and all pushes will overwrite your boot code. Also the combination of DS at 0x7C00 and Memory references based at 0x7C00 will cause all absolute label references in DS to expand to 0x83C00+Label relative to start of code.
0x83C00 is 0x7C00:0x7C00.
Post Reply