In: Computer Science
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
;; x86 intel 32 assembly language
section .data
        msg1 db "Even no. from 1 to 20",10,0
        msg db "%d",10,0
section .text
        global main   ;for gcc compiler we use function name main
        extern  printf  ;  printf is builtin function of c
main:
        push msg1
        call printf     ; pritnt the msg1
        add esp,4
        
        mov ecx,1       ;move 1 into ecx register for counter purpose
        mov ebx,2       ;move 2 into ebx for dividing by 2
        mov eax,1       ;eax stores the number which we are going to check even
        
lp1:    xor edx,edx      ; remainder is stored in edx register by default
        div  ebx         ;divide eax by ebx
        cmp edx,0        ;check remainder is zero or not
        jz printn        ; if remainder is zero then jmp to label printn and print that number
        
prev:   inc ecx         ; increment counter
        mov eax,ecx     ;get the next no in eax register
        cmp ecx,21      ;compair with 21
        jl lp1          ;if ecx is less that 21 then jump to lable lp1
        ret             ;if not  less then exit from code
        
printn: pusha              ;push all cureent values of registers into stack because after calling printf values get modified
        push ecx         ; push value of ecx into stack
        push msg        ;push msg  into stack
        call printf     ;call printf function
        add esp,8       ;add 8 in stack pointer i.e. esp register
        popa             ;pop all values of registeres that was push by pusha instruction
        jmp prev        ;jump to label prev to check next no.
