NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: tysonprogrammer on July 06, 2021, 09:24:07 PM
-
I have written many little EXE programs in assembly for DOS and they work great. I have been looking a few COM programs in assembly and do not see how one allocate space for the stack.
I tired using
section stack class=stack
rest 64
and I get uninitialized stack when I compile. This maybe normal I do not know.
any tips would be appreciated. The docs also do not show how to allocate stack space for a COM program.
thanks,
Tyson
-
MS-DOS COM programs have only ONE segment. On loading, CS=DS=ES=SS. You can ask for MS-DOS to allocate memory and use this space as stack if you want. Refer to Ralf Brown Interrupt List to see how can you do it.
-
Thanks I will look into that. I see some example where they define a variables and use that as their stack, I thought that was odd but I hadn't written a COM program yet.
Tyson
-
MS-DOS COM programs have only ONE segment. On loading, CS=DS=ES=SS. You can ask for MS-DOS to allocate memory and use this space as stack if you want. Refer to Ralf Brown Interrupt List to see how can you do it.
Segment registers are not necessarily the same at program start. It's best to set them where you want them at the start of your code. When setting up the stack in a com file, make sure that SS:SP points outside your code and data.
-
Segment registers are not necessarily the same at program start. It's best to set them where you want them at the start of your code. When setting up the stack in a com file, make sure that SS:SP points outside your code and data.
For COM files this behavior is standard for MS-DOS. All segments have the same value:
; Simple test showing segment selectors for COM files.
; nasm -fbin test.asm -o test.com
bits 16
org 0x100
lea dx,[msg1]
mov ah,9
int 0x21
mov ax,cs
call printhex
lea dx,[msg2]
mov ah,9
int 0x21
mov ax,ds
call printhex
lea dx,[msg3]
mov ah,9
int 0x21
mov ax,es
call printhex
lea dx,[msg4]
mov ah,9
int 0x21
mov ax,ss
call printhex
lea dx,[msg5]
mov ah,9
int 0x21
mov ax,0x4c00
int 0x21
printhex:
mov cl,12
.loop:
push ax
mov bx,ax
shr bx,cl
and bx,0x0f
mov al,[bx+hextbl]
mov [digit],al
lea dx,[digit]
mov ah,9
int 0x21
pop ax
sub cl,4
jnl .loop
ret
msg1: db 'cs=$'
msg2: db ', ds=$'
msg3: db ', es=$'
msg4: db ' ss=$'
msg5: db `\r\n$`
digit: db '0$'
hextbl: db '0123456789ABCDEF'
Compiling and executing:
C:\WORK> nasm -f bin test.asm -o test.com
C:\WORK> test
cs=0193, ds=0193, es=0193, ss=0193
-
I used MASM + Link before. In DOSBox, every segment I saw under Debug was different. Now that I use NASM, I have not understood why all segments of COM compiled are the same.Finally found the answer, now finally understand.Thank you, friends.
;D