简体   繁体   中英

How to fix “error:cannot generate COM file, stack segment present”?

I've been trying to fix my code for a while, but I can't seem to get it to work.It always tells me error:cannot generate COM file, stack segment present. Any ideas how I can fix this?

Here's my code:

.model small
.386
.stack 100h
.data
     inpM db "Input string: $"
     input db 19              ; max allowed 20
              db ?                ; # char entered
              db 19 dup(0)   ; chars entered
     output db 19 dup("$")
.code
start:    mov ax, @data
            mov ds, ax

            mov ah, 9              ; print inpM
            lea dx, inpM
            int 21h

            mov ah, 0Ah         ; get input
            lea si, input
            mov dx, si
            int 21h

            mov cl, [si+1]        ; reverse
            mov ch, 0
            add si, cx
            inc si
            lea di, output

rev:      mov al, [si]
            mov [di], al
            dec si
            inc di
            loop rev

again:  mov ah, 6         ; clrscr
            mov al, 0
            mov cl, 0
            mov ch, 0
            mov dl, 4Fh
            mov dh, 18h
           mov bh, 0Fh
           int 10h

          mov ah, 0
          mov bh, 0
          mov dl, 27h            ; column
          mov dh, 0               ; row

          mov ah, 9               ; print output
          lea dx, output
          int 21h

          mov bx, 20000       ; delay
l1:       mov cx, 0Fh

l2:      dec bx
          loop l2
          jnz l1

          add dh, 1                ; adds 1 to row

          loop again

          mov ah, 4Ch
          int 21h
end start

Additional info: My code reverses a string input then displays it in rows with a delay. Hoping to find out what's the cause of the error and how I'll be able to fix it.

I'm assuming from the error you are using TASM and TLINK to build this as a .COM program and not an EXE. Things you need to be aware of:

  • COM programs should use the tiny model, not small .
  • COM programs need to start with an ORG of 100h after you start the .code segment.
  • The stack in a COM program starts from the top of the segment the code runs in so setting the stack size needs to be removed.
  • You no longer need to set up DS as you would with building an EXE file since CS=DS=ES=SS with a COM program.

With those things in mind, you can modify the top of your code to appear like this:

.model tiny
.386
.data
     inpM db "Input string: $"
     input db 19              ; max allowed 20
              db ?                ; # char entered
              db 19 dup(0)   ; chars entered
     output db 19 dup("$")
.code
org 100h
start:
        mov ah, 9              ; print inpM
        lea dx, inpM
        int 21h

        ...

You can then build it with:

tasm myprg.asm
tlink /t myprg.obj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM