%macro print 4 ;macro for printing data mov rax,%1 ;func num for outputting data mov rdi,%2 ;file descriptor ID for output device(monitor) mov rsi,%3 ;starting address of the string mov rdx,%4 ;number of bytes to be displayed syscall %endmacro section .data ;initialized variable declaration msg db "Addition of the numbers is: " msgLen equ $-msg array db 12,3,4,2,1 ;declare the numbers in the array newline db "",10 section .bss ;uninitialized variable declaration sum resb 4 temp resb 4 section .text global _start ;entry point of the program _start: print 1,1,msg,msgLen mov rsi, array ;move the register source index to the first element in the array mov ax, 00h ;initialize contents of accumulator and register b to 0 mov bx, 00h mov cx, 5 ;set the counter to 5 up2: mov bl, byte[rsi] add ax, bx jnc skip inc ah skip: inc rsi ;go to the next location dec cx ;decrement the counter jnz up2 mov word[sum],ax mov bp, 4 mov ax, word[sum] up: ;simple hex to ascii conversion rol ax, 4 ; rotate left ax by 4 mov bx, ax ; store copy of ax in bx and ax, 0fh ; mask al to keep only 1 nibble cmp al, 09 ; compare accumulator contents with 09 jbe down ;if digit<= 9 go to 'down' add al, 07 ;else add 07H and then go to 'down' down: add al, 30h ;add 30H to the accumulator contents mov [temp], al print 1, 1,temp, 1 ;print the sum stored in temp mov ax, bx dec bp jnz up print 1,1,newline,1 ;print the newline character print 60,0,0,0 ;exit from the code(60 is the function number for sys_exit)
To embed this project on your website, copy the following code and paste it into your website's HTML: