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
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
Maybe it doesn't like the PUSH AX?
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.
I managed to fix the program by removing the PUSH AX, and setting the BP register value to SP, here is the code now:
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
but I still don't understand why the PUSH instruction causes this strange behaviour.
Quote from: Irog on June 08, 2017, 08:28:01 PMI still don't understand why the PUSH instruction causes this strange behaviour.
Maybe you should grab a manual and investigate what
call does to the stack?
I actually did, after hours of searching on google I finally found it, but thanks for pointing me in the right direction it really helped! :)