Thursday, 7 August 2014

NASM: addition of 3 numbers(for sum less than 10)

section .data
    input:db 'enter the number',10   ;
   
    inputLen:  equ $-input           ;
                                  ;
    display:db 'entered number is:',10   ;
   
    displayLen:  equ $-display       ;

       addition:db 'addtion of number is',10;
    additionLen: equ $-addition;

                                  ;


section .bss
a resb 3   
b resb 3
c resb 3
r resb 3


section .text
    global _start

_start:
    mov rax,1         ;string:enter number
    mov rdi,1            ;file discriptor
    mov rsi,input        ;input
    mov rdx,inputLen     ;lenght of input
                         ;
    syscall              ; Call the kernel

   
    mov rax,0         ;input
    mov rdi,0            ;standard input
    mov rsi,a       ;
    mov rdx,2          ;read only first two number
                         ;
    syscall              ;Call the kernel........

          
        mov rax,1         ;string2
    mov rdi,1            ;
    mov rsi,input        ;
    mov rdx,inputLen     ;
                         ;
    syscall              ; Call the kernel

    mov rax,0         ;input2
    mov rdi,0            ;
    mov rsi,b        ;
    mov rdx,2     ;
                         ;
    syscall              ; Call the kernel.....

    mov rax,1         ;string:enter number
    mov rdi,1            ;file discriptor
    mov rsi,input        ;input
    mov rdx,inputLen     ;lenght of input
                         ;
    syscall              ; Call the kernel
       
    mov rax,0         ;input3
    mov rdi,0            ;
    mov rsi,c        ;
    mov rdx,2     ;
                         ;
    syscall              ; Call the kernel.....

     



       
    XOR RAX,RAX ;addition of 3 numbers
    XOR RBX,RBX
    MOV AL,[a]
    sub al,30h
    MOV BL,[b]
    sub bl,30h
    MOV CL,[c]
    sub cl,30h
    ADD AL,BL
    ADD AL,CL
    ADD al,30h
    MOV [r],AL
syscall

    mov rax,1         ;string2
    mov rdi,1            ;
    mov rsi,addition      ;
    mov rdx,additionLen    ;
    syscall

    mov rax,1
    mov rdi,1
    mov rsi,r
    mov rdx,3


    syscall    
    mov rax,60                ; The system call for exit (sys_exit)
    mov rbx,0                ; Exit with return code of 0 (no error)
    syscall            ;

;tnx for using code from http://www.parazhar.blogspot.in

;OUTPUT=
;[apcoer@localhost ~]$ nasm -f elf64 3addps.asm
;[apcoer@localhost ~]$ ld -o 3addps 3addps.o
;[apcoer@localhost ~]$ ./3addps
;enter the number
;1
;enter the number
;2
;enter the number
;3
;addition of number is
;6






No comments:

Post a Comment