Jordan Means
Jordan Means

Reputation: 41

Implementing Arithmetic in Assembly

I am a new programmer for Assembly on Winx64. I'm attempting to set up code that divides the values of one array in the values of another using the basic arithmetic from Assembly programming. The lab I couldn't finish is this:

enter image description here

Here is the code I tried to implement (which was placed in the Hyper-V Virtual Machine, sorry for any poor indentation I end up having):

TITLE DISPLAY
      .MODEL SMALL
      .386
      .STACK
      .DATA
S     EQU 12 ;size of arrays
X     BYTE 4, 16, 100, -116, -68, -104, 125, 60, 99, 33, 55, 77
Y     BYTE 2, 3, 4, -5, -6, -7, -8, -9, -10, 11, 12, 13
Q     BYTE 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0     ;quotient = X/Y
R     BYTE 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0     ;remainder = X/Y

;Address of X is _______:_______
;Address of Y is _______:_______
;Address of Q is _______:_______
;Address of R is _______:_______

      .CODE
MAIN  PROC FAR
      .STARTUP
      ;Program
      MOV ESI, 0       ;use ESI and index to arrays
      MOV CX, S       ;counter for loop

L1:   MOV AX, 0 ;clear the AX register
      MOV AL, [X+ESI] ;load dividend
      MOV BL, [Y+ESI]
      MOV DX, 0 ;clear the DX register
      IDIV AX ;divide X by Y
      MOV [Q+ESI], EAX    ;store quotient in Q
      INC ESI  ;increment index by 1
      LOOP L1
      .EXIT 
MAIN  ENDP
      END

The first issue I'm having is that Line 29 has Error A2070: Invalid Instruction Operands. The second issue is that I'm not sure whether the entirety of the code is correct along with the error. I believe I am able to find the values of X and Y (not Q and R particularly) in the memory of the debugger, but I'm having trouble assembling this code first.

Here is also an Excel except with the expected values for Q and R.

enter image description here

Upvotes: 1

Views: 434

Answers (1)

Jordan Means
Jordan Means

Reputation: 41

Alrighty. The issue, found out by fuz, is that I stored the quotient incorrectly.

TITLE DISPLAY
      .MODEL SMALL
      .386
      .STACK
      .DATA
S     EQU 12 ;size of arrays
X     BYTE 4, 16, 100, -116, -68, -104, 125, 60, 99, 33, 55, 77
Y     BYTE 2, 3, 4, -5, -6, -7, -8, -9, -10, 11, 12, 13
Q     BYTE 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0     ;quotient = X/Y
R     BYTE 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0     ;remainder = X/Y

;Address of X is _______:_______
;Address of Y is _______:_______
;Address of Q is _______:_______
;Address of R is _______:_______

      .CODE
MAIN  PROC FAR
      .STARTUP
      ;Program
      MOV ESI, 0       ;use ESI and index to arrays
      MOV CX, S       ;counter for loop

L1:   MOV AX, 0 ;clear the AX register
      MOV AL, [X+ESI] ;load dividend
      MOV BL, [Y+ESI]
      MOV DX, 0 ;clear the DX register
      IDIV AX ;divide X by Y
      MOV [Q+ESI], AL   ;store quotient in Q
      INC ESI  ;increment index by 1
      LOOP L1
      .EXIT 
MAIN  ENDP
      END

Upvotes: 1

Related Questions