NO

Author Topic: 8086 Assembly question  (Read 3609 times)

Irog

  • Guest
8086 Assembly question
« on: June 08, 2017, 05:29:36 PM »
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: [Select]
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

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: 8086 Assembly question
« Reply #1 on: June 08, 2017, 06:31:56 PM »
Maybe it doesn't like the PUSH AX?

Irog

  • Guest
Re: 8086 Assembly question
« Reply #2 on: June 08, 2017, 06:55:21 PM »
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

  • Guest
Re: 8086 Assembly question
« Reply #3 on: June 08, 2017, 08:28:01 PM »
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: [Select]

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.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: 8086 Assembly question
« Reply #4 on: June 09, 2017, 09:24:43 AM »
I 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?

Irog

  • Guest
Re: 8086 Assembly question
« Reply #5 on: June 10, 2017, 09:40:55 AM »
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!  :)