Monday, September 8, 2014

Calculate perimeter of rectangle and square with NASM

Calculate perimeter of rectangle and square with NASM(64 bit Linux):

%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;macro for printing
%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;macro for accepting

SECTION .data

msg: db"Enter length of the square::",0xA
len: equ $-msg

msg1: db"Enter base of the rectangle::",0xA
len1: equ $-msg1

msg2: db"Enter the height of the rectangle::",0xA
len2: equ $-msg2

msg3: db"The perimeter of the square is:: ",0xA
len3: equ $-msg3

msg4: db"The perimeter of the Rectangle is:: ",0xA
len4: equ $-msg4

menu: db 0xA,"1.Calculate Square Perimeter",0xA,"2.Calculate Rectangle Perimeter",0xA,"3.Exit",0xA
menulen: equ $-menu

SECTION .bss
choice: resb 2    ;for choice of menu
leng: resb 2    ;for length of the square
base: resb 4    ;for base of the rectangle
height : resb 2    ;for height of the rectangle
peri : resb 4    ;for permeter of square
perirect : resb 4
res1 :resb 4

res2: resb 4
resl: resb 1
resm: resb 1
count: resb 1
tempnum: resb 1


SECTION .text

global _start:
_start:

menuprint:

print menu,menulen        ; accepting choice
read choice,2

cmp byte[choice],31h
je PerimeterofSquare

cmp byte[choice],32h
je PerimeterofRectangle

cmp byte[choice],33h
je EXIT


PerimeterofSquare:
        print msg,len
        read leng,10     ; reading the number
           
            convert_lengths:    ;converting from ascii to hex
            mov bl,00
            mov esi,leng
            mov ecx,2       
            loop1:   
                rol bl,04
                mov al,[esi]
                cmp al,39h
                jbe loop2
                sub al,07h
            loop2:
                sub al,30h
                add bl,al
                inc esi
            loop loop1
    mov byte[resl],00            ; initializing all variables
    mov byte[resm],00
    mov byte[tempnum],00
    mov [tempnum],bl
    mov al,[tempnum]
    mov byte[count],4
    addp:
        add[resl],al            ;successive addition of the number for 4 times caz
        adc byte[resm],00        ; perimeter of the square=4*length
        dec byte[count]
        jnz addp

    mov bl,[resl]
    mov bh,[resm]                               
    mov [peri],bx
    convert_result:                ;converting the result into the hex   
            mov esi,peri
            mov ecx,4
            loop3:
                rol bx,04
                mov al,bl
                and al,0Fh
                cmp al,09h
                jbe loop4
                add al,07h
            loop4:
                add al,30h
                mov [esi],al
                inc esi
            loop loop3       
    print msg3,len3
    print peri,4
jmp menuprint

PerimeterofRectangle:
           
            print msg1,len1
            read base,10        ;accepting the base
               
                mov ecx,2    ;converting ascii to hex
                mov esi,base
                mov bl,00
            loop5:
                rol bl,04
                mov al,byte[esi]
                cmp al,39h
                jbe loop6
                sub al,07h
            loop6:
                sub al,30h
                add bl,al
                inc esi
            loop loop5
    mov byte[resl],00        ; initializing the data
    mov byte[resm],00
    mov byte[tempnum],00
    mov [tempnum],bl
    mov al,[tempnum]
    mov byte[count],2
    addp1:
        add[resl],al
        adc byte[resm],00
        dec byte[count]
        jnz addp1        ; successive addition for two times
                    ; base=base+base
    mov bl,[resl]            ; height=height+height
    mov bh,[resm]
    mov [res1],bx
    ;add byte[res1],30h
    ;print res1,4
       
        print msg2,len2
        read height,4
                mov ecx,2
                mov esi,height
                mov bh,00
            loop7:
                rol bh,04
                mov ah,byte[esi]
                cmp ah,39h
                jbe loop8
                sub ah,07h
            loop8:
                sub ah,30h
                add bh,ah
                inc esi
            loop loop7

    mov byte[resl],00
    mov byte[resm],00
    mov byte[tempnum],00
    mov [tempnum],bh        ;Similar operations on height
    mov al,[tempnum]
    mov byte[count],2
    addp2:
        add[resl],al
        adc byte[resm],00
        dec byte[count]
        jnz addp2

    mov bl,[resl]
    mov bh,[resm]
    add bx,[res1]        ;adding the both results
    mov [res2],bx
    ;add byte[res2],30h
    ;print res2,4
   
       
            mov ecx,4
            mov rsi,perirect
            loopl:
                rol bx,04
                mov al,bl
                and al,0Fh
                cmp al,09h
                jbe loopl2
                add al,07h
            loopl2:
                add al,30h
                mov [rsi],ax
                inc rsi        ;converting hex to ascii
            loop loopl
    print msg4,len4   
    print perirect,4
jmp menuprint
EXIT:
mov rax,60
mov rdi,0
syscall

No comments:

Post a Comment

Home Java Python PHP