Author Topic: DOS COM Program Stack  (Read 4490 times)

Offline tysonprogrammer

  • Jr. Member
  • *
  • Posts: 6
  • Country: us
  • C# application developer attempting assembly.
DOS COM Program Stack
« 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

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 171
  • Country: br
Re: DOS COM Program Stack
« Reply #1 on: July 06, 2021, 10:04:30 PM »
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.

Offline tysonprogrammer

  • Jr. Member
  • *
  • Posts: 6
  • Country: us
  • C# application developer attempting assembly.
Re: DOS COM Program Stack
« Reply #2 on: July 06, 2021, 10:20:22 PM »
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

Online debs3759

  • Global Moderator
  • Full Member
  • *****
  • Posts: 136
  • Country: gb
    • GPUZoo
Re: DOS COM Program Stack
« Reply #3 on: July 06, 2021, 11:09:21 PM »
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.
My graphics card database: www.gpuzoo.com

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 171
  • Country: br
Re: DOS COM Program Stack
« Reply #4 on: July 07, 2021, 12:39:58 PM »
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:
Code: [Select]
  ; 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:
Code: [Select]
C:\WORK> nasm -f bin test.asm -o test.com
C:\WORK> test
cs=0193, ds=0193, es=0193, ss=0193

Offline gdf87521

  • New Member
  • Posts: 1
Re: DOS COM Program Stack
« Reply #5 on: July 08, 2021, 06:15:27 AM »
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