News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Help with creating a loop in MASM

Started by revolution9540, September 19, 2015, 05:27:08 AM

Previous topic - Next topic

revolution9540

I am programming assembly language (x86) in MASM using Visual Studio 2013 Ultimate. I am trying to use an array to calculate a Fibonacci sequence for n elements using an array. In other words, I am trying to go to an array element, obtain the two elements before it, add those up, and store the result in another array.

I am having trouble setting up the index registers to make this work.

I have my program setup like this:

TITLE fibonacci.asm

INCLUDE Irvine32.inc

.data
fibInitial BYTE 2, 3, 4, 5, 6
fibComputed BYTE 5 DUP(0)
fibOne WORD 0
fibTwo WORD 0

.code
main PROC

MOVZX si, fibInitial
MOVZX di, fibComputed
MOV   cl, LENGTHOF fibInitial

L1:
MOV   ax, [si - 1]
MOV   dx, [si - 2]
MOV   bp, ax + dx
MOV   dl, TYPE fibInitial
MOVZX si, dl
MOV   [edi], bp
MOV   dh, TYPE fibComputed
MOVZX di, dl
loop L1

exit
main ENDP
END main



I cannot compile this because of an error message that says "error A2031: must be index or base register" for the line MOV ebp, ax + dx

dedndave

i didn't really examine the code....

try this line
    lea     bp,[ax+dx]          ;loads BP with AX + DX

don't know how that will go in 16-bit code   :P

on a side note....

you probably don't need to keep the entire array in memory
in fact, if you use registers carefully, you might be able to carry the last 2 values in register

rrr314159

hi revolution9540,

Your program won't work as it stands. You don't want two arrays, just one, which will continuously fill up with fib numbers. The array should start like this:

1,2, 0, 0, 0, 0, ...

and end like this:

1,2,3,5,8,13,21,34,55,89,144,233 (next one, 377, won't fit in a byte)

It should look something like this:

NUMBER_FIBS_TO_COMPUTE = 10
.data
  fib BYTE 1,2
      BYTE NUMBER_FIBS_TO_COMPUTE dup(0)
.code

main PROC
  lea esi, [fib+2]
  mov cl, NUMBER_FIBS_TO_COMPUTE
@@:
  mov al, [esi-2]
  add al, [esi-1]
  mov [esi], al
  inc esi
  loop @B

; here print out the results or examine them with debugger

exit
main ENDP
END main


I haven't run this, may be some stupid mistake in it, but hopefully u get the idea
I am NaN ;)