MITL: Assignment-1

section .data

tname db "tnx for using code from parazhar.blogspot.com",10
lname equ $-tname
tcnt db "how many numbers do you wanna add? :-"
lcnt equ $-tcnt
tnum db "Enter number:-"
lnum equ $-tnum
tsum db "the addition of numbers is:-"
lsum equ $-tsum
;________________________________________________________________________
section .bss

numcnt resb 2
number resb 17
sum resq 1 ;reserve quadword for storing 64 bit result

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;________________________________________________________________________
section .text
global _start:
_start:

write tname,lname ;displays name
write tcnt,lcnt ;disp enter count
read numcnt,2 ;accepts numcnt

mov qword[sum],0 ;initialize val of sum to 0

;____________________________________________________________________
;number conversion
;____________________________________________________________________
;cmp byte[numcnt],7h
;jg onine
;sub byte[numcnt],7h
;onine: cmp byte[numcnt],30h
;jg AZ
;sub byte[numcnt],30h
;AZ: cmp byte[numcnt],37h
;sub byte[numcnt],37h
sub byte[numcnt],30h
;___________________________________________________________________
hexin:
write tnum,lnum
read number,17 ;accept hex number

mov ch,10h ;initialize counter values
mov cl,04h
xor rbx,rbx ;clear rbx
mov rdi,number ;moves offset address of number to rdi

loop:
shl rbx,cl ;shift left rbx by cl(where cl=4)
mov al,[rdi] ;take first byte from the address of rdi to accumulator
cmp al,39h
jng less
sub al,7h
less:
sub al,30h
and al,0Fh
add bl,al
inc rdi
dec ch
jnz loop
add qword[sum],rbx
dec byte[numcnt]
jnz hexin

mov ch,16
mov cl,4

mov rsi,number
mov rbx,qword[sum]

ASCII:
rol rbx,cl
mov al,bl
and al,0Fh
cmp al,09h
jng lesh
add al,07h
lesh:
add al,30h
mov [rsi],al
inc rsi
dec ch
jnz ASCII
write tsum,lsum
write number,16

exit:
mov rax,3ch
mov rdi,00
syscall


;______________________________________________________________________
;output
;______________________________________________________________________
;[Aqua-4@localhost mitl]$
;[Aqua-4@localhost mitl]$ nasm -f elf64 a1.asm
;[Aqua-4@localhost mitl]$ ld -o assignment1 a1.o
;[Aqua-4@localhost mitl]$ ./assignment1
;tnx for using code from parazhar.blogspot.com
;how many numbers do you wanna add? :-2
;Enter number:-000000125E125F0
;Enter number:-0001521264215FF
;the addition of numbers is:-00015224C233BF04
;----------------------------------------------------------------------

Comments

Popular posts from this blog

NASM: program to find largest number from an array of 32-bit numbers(hard-coded)

Rules for drawing a FLOWCHART