Reputation: 534
I've had an assembly (MASM x86) code for doing this C++ equation
void main()
{
unsigned int i, B[10], A[10]={3,2,3,1,7,5,0,8,9,2};
for (i=0; i<10; i++){
B[i] = A[i] + 2 * (2*i + ( (3*i+1) / 5) );
}
it's working when I use 32bit registers and variables but after changing them to use 16bit it returns some errors.
The assembly code
.386 ; Specify instruction set
.model flat, stdcall ; Flat memory model, std. calling convention
.stack 4096 ; Reserve stack space
ExitProcess PROTO, dwExitCode: WORD ; Exit process prototype
.data
A WORD 3,2,3,1,7,5,0,8,9,2
B WORD 10 DUP (?)
var1 WORD 0
quotient WORD 0
; A[i] + 2 * (2*i + ( (3*i+1) / 5) )
.code
main PROC
mov si, 0
loop_start:
;(3*i+1)
mov ax, 3
mul si
inc ax
; / 5)
mov dx,0
mov cx,5
div cx
mov quotient,ax
; 2*i
mov ax,2
mul si
; (2*i + ( (3*i+1) / 5) )
mov bx, quotient
add ax,bx
; 2 * (2*i + ( (3*i+1) / 5) )
mov var1,2
mul var1
mov var1, ax
; load array A
lea bx,A
mov cx,4
;A[i] + 2 * (2*i + ( (3*i+1) / 5) )
;imul dx,si
;mov bx, dx
mov ax,[bx + TYPE A * si]
add bx,var1
;load array B and store
lea ax,B
mov [ax + dx], bx
; i<10; i++
inc si
cmp si, 10
jl loop_start
INVOKE ExitProcess, 0 ; call exit function
main ENDP ; exit main procedure
END main ; stop assembling
The error I'm getting is error A2031: Must be index or base register
on mov ax,[bx + TYPE A * si]
when I'm trying to read array index
As far as I know the bx is a base registry, I also add bx
with si
outside of the []
but still doesn't work!
Update
Is there anyway to run fix this code without using 32bit indexes, registers and variables?
Upvotes: 0
Views: 40