Search code examples
assemblyinterfacex86-16x87

Calculating tangent of a provided angle in 8086/8088 using 8087 coprocessor


I have a project that I'm supposed to finish, the project is to write a 8086/8087 based program using 8087 coprocessor to find the tangent of an angle. The angle should be in degrees and print an output of tan(angle)

What I did so far is:

.model small
.stack 100h

.data

angle   dd      0.0
TanX    dd      0.0

.code
mov ax,@data
mov ds,ax


    fld     long[angle]            ; st(0) = angle
    fsincos                         ; st(0) = cos(angle);  st(1) = sin(angle)
    fdivrp  st1, st0                ; st(0) = st(1) / st(0)   ( = sin/cos )
    fstp    long [TanX]             ; TanX = tan(angle)

end

Is there anything I'm doing wrong? And how can I improve what I wrote?


Solution

  • ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; Name: Mohammad Tayseer Mohammad Abu Mailiesh
    ; Date: April, 19th 2019
    ; Project: Tangent calculator
    ; Overview: Asm program that finds the tangent of a defined angle
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;
    
    
    
    .model small
    .stack 100h
    
    .data
    
    angle    dd      40.0 ;angle in degrees desired to be computed 
    oneighty dd      180.0
    var      dd      1.0 
    tanx1    dd      0.0 
    tanx     dd      0.0
    angle1   dd      0.0
    
    
    .code
    main proc far
    mov ax,@data
    mov ds,ax
    
    finit ;initialize FPU after checking for pending unmasked floating-point exceptions
    
    ;;;;;;;;;;;;;;;;;;;;finding and calculating where the angle lies and change it to an angle that lies inbetween 0 and 45;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                mov ax, angle
                cmp ax, 90.0
            JA B2
                cmp ax, 45.0
            JB B1
                mov ax, 90.0
                SUB ax, angle
            Jmp B1
    
     B2:        cmp ax, 180.0
                JA B3
                mov ax, 180.0
                SUB ax, angle
            Jmp B1
    
     B3:        cmp ax, 270.0
                JA B4
                mov ax, angle
                SUB ax, 180.0
            Jmp B1
    
     B4:        mov ax, 360.0
                SUB ax, angle
            Jmp B1
    
    
     B1:    cmp ax, 45.0
            JB S1
            mov dx, ax
            mov ax, 90.0
            SUB ax, dx
            Jmp S1
    
     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; changing angle from degrees to radian ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
     S1:    mov angle1, ax
            fld oneighty 
            fld angle1
            fldpi
            fmul
            fdiv
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; computing tangent of the angle ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            fld angle ;st(0) = angle
            fptan ;st(0) = cos(angle), st(1) = sin(angle)
            fwait
            fstp tanx ;tanx = tan(angle)
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; if the value is in the other half of first box (from 45 to 90) then 1/tanx to get the right value ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
            CMP dx, 45
            JB E1
            fild var
            fild tanx
            fdiv
            fwait
            fstp tanx1
            Jmp E2
    
    
     E1:    mov ah,4ch ;end the program
            int 21h
    
     E2:    mov ah,4ch ;end the program
            int 21h
    
    main endp
    end main
    

    This is the full solution for the attached problem if anyone is looking for it. Thank you everyone!