Ads

Thursday 10 October 2013

Microprocessor Architecture -- AREA OF CIRCLE

;Name:Venkatesh Lokare
;Roll No:2431
;Title:Program to calculate area of circle using(co-processor)
======================================================================


%macro print 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro

SECTION .data
round dd 1000.00
rad dd 100.00
msg1 db 0ah, "Program to calculate area of circle using coprocessor instructions",0ah
msg1len equ $-msg1
msg2 db "Area is = "
msg2len equ $-msg2
dot db "."
newln db 0ah

SECTION .bss
intval: rest 01
decval: rest 01
res: resd 01
cwreg: resw 01
digits: resb 20
cnt: resd 01

SECTION .text
GLOBAL _start
_start:
print msg1,msg1len

finit
fld dword [rad] ;st0 = radius
fmul st0,st0 ;st0 = radius^2
fldpi ;st0 = pi, st1=radius^2
fmul st0,st1 ;st0 = pi*radius^2
fstp dword [res]

fstcw word [cwreg]
mov ax,[cwreg]
or ax,0c00h
mov word [cwreg],ax ;rounding off
fldcw word [cwreg]

fld dword [res] ;st0 = result
frndint ;LHS of dec. point
fbstp tword [intval] ;pop BCD

fbld tword [intval]
fld dword [res] ;st0 = area
fsub st0,st1 ;st0 = st0-st1
fld dword [round] ;st0=100.00
fmul st0,st1

frndint ;RHS of dec. point
fbstp tword [decval]

print msg2,msg2len

mov esi,intval ;convert LHS BCD to ASCII string
;and display LHS
mov edi,digits
call convert
print digits,dword[cnt]

print dot,1 ;display dot

mov esi,decval ;convert RHS BCD to ASCII string
;and display LHS
mov edi,digits
call convert
print digits,dword[cnt]

print newln,1

mov eax,1 ;exit
mov ebx,0
int 80h
;.................................................................
convert:
mov ecx,10 ; no of bytes
mov ebx,0 ;count actual bytes
add esi,09

again:
mov al,[esi]
cmp al,00
jne cont
jmp next

cont:
and al,0F0h ;higher nibble
shr al,4
add al,30h ;its BCD, so just add 30h
;for ASCII

mov [edi],al
inc edi
inc ebx

mov al,[esi]
and al,0Fh ;lower nibble
add al,30h

mov [edi],al
inc edi
inc ebx

next:
dec esi
dec ecx
jnz again

mov [cnt],ebx
ret
;...................................................

OUTPUT:
[admin@localhost ~]$ nasm -f elf64 d1.asm
[admin@localhost ~]$ ld -s -o d1 d1.o
[admin@localhost ~]$ ./d1

Program to calculate area of circle using coprocessor instructions
Area is = 031415.0925
[admin@localhost ~]$


No comments:

Post a Comment