Assembly language > Assembly discussions

8086 Assembly question

(1/2) > >>

Irog:
I've been playing around with 8086 assembly, I decided to make a simple program that takes 2 arrays defined in the data segment and pushes all the members onto the stack but for some reason calling a procedure causes an infinite loop when I use the ret instruction inside it.

Here's the code


--- Code: ---ORG 100H

DATA SEGMENT
   
    ARRAY DB 1, 4, 7, 8
   
    ARRAY2 DB 2, 3, 6, 9
     
DATA ENDS

CODE SEGMENT
     ASSUME CS:CODE,DS:DATA

         
      MOV AX, @DATA
      MOV DX, AX
     
      LEA SI, ARRAY
     
      CALL LOOPTHRU
     
      LEA SI, ARRAY2
     
      CALL LOOPTHRU
     
      MOV AH, 0   ;EXIT
      INT 21H     ;PROGRAM
     
     
LOOPTHRU PROC
   
      MOV AX, 0
      MOV BX, 0
   
_LOOP:
      MOV AL, [SI]
      INC SI
      INC BX
      PUSH AX
     
      CMP BX, 4
      JNE _LOOP
     
      RET ;Calling this goes back to the beginning of the program instead of continuing at the instruction after the call
LOOPTHRU ENDP


   
CODE ENDS
END

--- End code ---

jj2007:
Maybe it doesn't like the PUSH AX?

Irog:
I have removed the push ax and it works now, but now another question arises,  why does it behave so strange with the push instruction? Also thanks for helping me find the problem , I never would've gussed that it was the push instruction that caused the problem, I'm not very skilled with assembly.

Irog:
I managed to fix the program by removing the PUSH AX, and setting the BP register value to SP, here is the code now:


--- Code: ---
DATA SEGMENT
   
    ARRAY DB 1, 4, 7, 8
   
    ARRAY2 DB 2, 3, 6, 9
     
DATA ENDS

STACK_SEG SEGMENT PARA STACK
   
    DB 64 DUP (0)
   
STACK_SEG ENDS

CODE SEGMENT
     ASSUME CS:CODE,DS:DATA, SS:STACK_SEG
     
MAIN PROC FAR
         
      MOV AX, @DATA
      MOV DS, AX
     
      MOV BP, SP
      MOV DI, 0
     
      LEA SI, ARRAY
     
      CALL LOOPTHRU     
     
      LEA SI, ARRAY2
     
      CALL LOOPTHRU
     
      MOV AH, 0
      INT 21H   
      RET
     
MAIN ENDP
     
     
LOOPTHRU PROC NEAR
   
      MOV AX, 0
      MOV BX, 0
   
_LOOP:
      MOV AL, [SI]
      MOV [BP-DI], AX
      INC SI
      INC BX
      SUB DI, 2
     
      CMP BX, 4
      JNE _LOOP
     
      RET
     
LOOPTHRU ENDP


   
CODE ENDS
END MAIN


--- End code ---

but I still don't understand why the PUSH instruction causes this strange behaviour.

jj2007:

--- Quote from: Irog on June 08, 2017, 08:28:01 PM ---I still don't understand why the PUSH instruction causes this strange behaviour.
--- End quote ---

Maybe you should grab a manual and investigate what call does to the stack?

Navigation

[0] Message Index

[#] Next page

Go to full version