Attempting to get this to print 1,2,3,...[value] in assembly

时间:2015-07-28 15:41:22

标签: assembly printf nasm

[value] is equal to 5. I've got it to where it prints

25367 ,

instead of

1,2,3,4,5,

I've commented my code below to show what I'm trying to do.

printnum:                       ;this section sets up the starting values
    mov edi, 0                  ;start at 0
    mov bl, [value]             ;"preserve" value
    jmp printnumloop            ;go to printnumloop


printnumloop:
     ;call  correct             ;for debugging to see if it reached here  
     cmp    edi,  [value]       ; compare the counter variable to [value]
     jle    actuallyprint       ; if less than or equal to, jmp to print
     jmp    menu                ;else, should be done printing, back to menu

actuallyprint:

    lea rdi, [rel formatnum]        ;loads "%d",0
    mov rsi, [rel edi]              ;should load value in edi
    mov al, 0
    call    printf                  ;prints number

    ;call   correct                  ;for debugging (check if here)

    lea     rdi, [LC8]               ;loads comma
    mov     eax, 0
    call    printf                   ;prints comma
    add     edx, 1                   ;add 1 to edx to print/check the next #

    call    printnumloop             ;calls printnumloop to check if <=value

New code! Progress! Get an infinte loop of numbers printed! The numbers appear negative though when adding 1, so it keeps going since it's jle after cmp counter to [value]. Tried inc edi too.

printnum:
    mov edi, 0                  ;start at 0
    jmp actuallyprint   


printnumloop:
    add edi, 1  
    cmp     edi, [value]
    jle     actuallyprint
    jmp menu


actuallyprint:

    mov esi, edi    
    lea rdi, [rel formatnum]
    mov al, 0
    call    printf

    lea rdi, [LC8]
    mov     eax, 0
    call    printf

    jmp printnumloop

1 个答案:

答案 0 :(得分:0)

Changed the counter variable to ebx and it works flawlessly.