0

This is a code to add all numbers between 50 and 150 and display the result in decimal form.I have created the stack segment .STACK 32 to store the remainders to convert the hex result to decimal format. The address of data segment is passed to DS. I also need to pass address of stack segment to SS but the error occurs.enter image description here

 TITLE ADD 10 NUMBERS FROM 50 TO 150
.MODEL SMALL
.STACK 32
.DATA
    ARR  DW 60,70,86,79,70,140,130,120,100,150
    STRING  DB "THE SUM OF NUMBERS IS: $"
    SUM DW ?
.CODE
    MAIN PROC FAR
           MOV  AX,@DATA
           MOV  DS,AX

           MOV  AX,@STACK
           MOV  SS,AX

           LEA  SI,ARR
           MOV  AX,0
           MOV  CX,10
    REPEAT:
           ADD  AX,[SI]
           INC  SI
           INC SI
           LOOP REPEAT

           MOV DX,0
           MOV CX,0
           MOV BX,10

    REPEAT2:
           CMP AX,0
           JZ NEXT
           DIV BX
           PUSH DX
           SUB DX,DX
           INC CX
           JMP REPEAT2

    NEXT:
            CMP CX,0
            JZ EXIT
            POP DX
            ADD DX,30H
            DEC CX
            MOV AH,02H
            INT 21H
            JMP NEXT

    EXIT:
           MOV  AX,4C00H
           INT  21H


MAIN ENDP
    END MAIN

It works for @data but not for @stack why?

7
  • You don't need it. SS will be set to the stack segment before control is transferred to your code. Commented Mar 4, 2024 at 4:53
  • 3
    The stack (SP:SP) is already set up by the DOS program-loader, with a size specified by the executable's metadata. You don't need to run instructions to set it up, so I assume there isn't support for a relocation for that, and the other pieces of toolchain support in the assembler. (If you were going to set a new stack pointer, you'd always want to set SP as the next instruction after setting SS, in case an interrupt happens then.) 64K of stack space should be enough for any DOS program (allocate memory from elsewhere for big objects), so there isn't support for multiple stack segments. Commented Mar 4, 2024 at 4:53
  • (If any of that informed-guesswork in my previous comment is wrong, I'm sure some of the usual suspects will be along to catch any mistakes. Paging @ ecm, @ MichaelPetch, @ fuz. (I know notifications don't actually work for people who haven't commented.)) Commented Mar 4, 2024 at 4:57
  • 2
    @PeterCordes I'm not entirely sure about MASM style assemblers/linkers. I believe the .STACK directive sets the stack size. 512 would probably be better than 32. However, you're right that generally DOS sets up the stack for an application before transferring control to the application's entry point. Multiple stacks are possible, but unlikely to be needed. And like you wrote, if you set ss you should set sp in the very next instruction. (Surrounding a stack change with cli and sti will do no harm either.) Commented Mar 4, 2024 at 8:40
  • 2
    @PeterCordes is correct that SS:SP is setup by the DOS loader so if you have one stack you don't need to set it (The DOS program loader will handle it). @Nassau is also correct that @stack didn't appear until MASM6, although if need be you could still use the SEG equate like mov ax, SEG stack. Another one that didn't exist in MASM5 was @const Commented Mar 4, 2024 at 22:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.