Task

Start an integer value at   1024.

Loop while it is greater than zero.

Print the value (with a newline) and divide it by two each time through the loop.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreachbas
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


 
; NASM 64 bit X86-64 assembly on Linux
 
global main
extern printf
 
segment .data
 
printffmt db `%ld\n`,0
 
segment .text
 
main:	                     
    push rbp                    
    mov rbp,rsp 
 
; used rbx and r12 because printf preserves these values
 
    mov rbx,1024                 ; start with 1024
    mov r12,2                    ; load 2 as divisor
 
.toploop                         ; top of while loop    
    cmp rbx,0                    ; compare to 0
    jle .done                    ; exit 0 or less
 
    lea rdi,[printffmt]          ; print number in rsi
    mov rsi,rbx                  ; mov to rsi as argument
    call printf
 
; calculate n/2 and save
    xor rdx,rdx                  ; clear rdx for division
    mov rax,rbx                  ; mov number to rax for division
    idiv r12                     ; divide by 2
    mov rbx,rax                  ; save n/2
 
    jmp .toploop                 ; next loop
 
.done
    xor rax,rax                  ; return code 0
    leave                        ; fix stack
    ret                          ; return
 