Search code examples
assemblyx86masm

My first exercise in assembly


I'm working under visual studio 2005 with assembly (I'm a newbie) and I want to create a program that calculate arithmetic progression with that rule: An = 2*An-1 + An-2 but. I really don't know how to work with registers and I need just one example from you to continue with my exercises.

This is my code:

.386
.MODEL flat,stdcall

.STACK 4096

extern ExitProcess@4:Near

.data                               
arraysize DWORD 10

setarray  DWORD 0 DUP(arraysize)
firstvar  DWORD 1
secondvar DWORD 2

.code                               
_main:                              
mov eax,[firstvar]
mov [setarray+0],eax        
mov eax,[secondvar]
mov [setarray+4],eax

mov ecx, arraysize              ;loop definition
mov ax, 8

Lp:
mov eax,[setarray+ax-4]
add eax,[setarray+ax-4]
add eax,[setarray+ax-8]
mov [setarray+ax],eax

add ax,4;
loop Lp

add ax,4;

    push    0                   ;Black box. Always terminate
    call    ExitProcess@4       ;program with this sequence

    end   _main              ;End of program. Label is the entry point.

Solution

  • You can't use ax as index register and eax as data register at the same time. For 32bit code, stick to 32 bit registers, unless you now what you are doing. You inadvertedly used a 16 Bit addressing mode, which you probably didn't want.

    mov ecx, arraysize-1              ;loop definition
    mov ebx, 8
    
    Lp:
    mov eax,[setarray+ebx-4]
    add eax,[setarray+ebx-4]
    add eax,[setarray+ebx-8]
    mov [setarray+ebx],eax
    
    add ebx,4
    dec ecx
    jnc Lp
    

    Never ever use the loop instruction, even if some modern processors can execute ist fast (most can't).