Advertisement
AVONnadozie

PROGRAM TO CALCULATE AREA AND CIRCUMFERENCE OF A CIRCLE(ASM)

Mar 20th, 2013
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;AUTHOR: ANUEBUNWA VICTOR OGECHUKWU
  2. ;PROGRAM TO CALCULATE AREA AND CIRCUMFERENCE OF A CIRCLE IN 8086 ASSEMBLY
  3. ;ASSEMBLER: MASM
  4.  
  5. .MODEL SMALL
  6. .STACK
  7. .DATA
  8.             PIx100 EQU 314          ;PI(approximated to 2dp) * 100
  9.             MSG1 DB "Enter radius: ",'$'
  10.             MSG2 DB 0AH,0DH,"The area of the circle is: ",'$'
  11.             MSG3 DB 0AH,0DH,"and it's circumference is: ",'$'
  12.             AREA DW ?
  13.             CIRCUMFERENCE DW ?
  14.             VALUE DW ?
  15. .CODE
  16. start:      MOV AX, @data
  17.             MOV DS, AX
  18.            
  19.             MOV AH, 09H     ;Print string function
  20.             LEA DX, MSG1
  21.             INT 21H
  22.             MOV AH, 3FH     ;Input string function
  23.             LEA DX, VALUE
  24.             INT 21H
  25.             LEA SI, VALUE
  26.             CALL CHAR2INTR      ;Converts collected characters to an integer and stores result in AX
  27.            
  28.                         ;Calculating Area
  29.             MOV VALUE, AX
  30.             MUL VALUE       ;raduis * radius
  31.             MOV BX, PIx100
  32.             MUL BX          ;(radius * radius) * (PI*100)
  33.             MOV BX, 100     ;Subroutine displayReal requires that you multiply value by 100 before division (to move value dp up 2 places)
  34.             MUL BX          ;i.e 4.230 * 100 = 423.0 (Moving dp up 2 places)
  35.                         ;dp will be placed at its appropriate position during display(in subroutine displayReal)
  36.             MOV BX, 100
  37.             DIV BX          ;(radius * radius) * (PI*100)/100 = (radius * radius) * PI
  38.             MOV AREA, AX        ;Stores result
  39.            
  40.                         ;Calculating Circumference
  41.             MOV AX, VALUE
  42.             MOV BX, 2
  43.             MUL BX          ;raduis * 2
  44.             MOV BX, PIx100
  45.             MUL BX          ;(radius * 2) * (PI*100)
  46.             MOV BX, 100
  47.             MUL BX          ;Moving dp up 2 places
  48.             MOV BX, 100
  49.             DIV BX          ;(radius * 2) * (PI*100)/100 = (radius * 2) * PI
  50.             MOV CIRCUMFERENCE, AX   ;Stores result
  51.            
  52.             MOV AH, 09H
  53.             LEA DX, MSG2
  54.             INT 21H
  55.             MOV AX, AREA
  56.             CALL DISPLAYREAL    ;Displays Area
  57.            
  58.             MOV AH, 09H
  59.             LEA DX, MSG3
  60.             INT 21H
  61.             MOV AX, CIRCUMFERENCE
  62.             CALL DISPLAYREAL    ;Displays Circumference
  63.            
  64.            
  65.             MOV AH, 4CH
  66.             INT 21H         ;Program terminates
  67.            
  68. CHAR2INTR PROC
  69.             PUSH BX
  70.             PUSH CX
  71.             PUSH DX
  72.             PUSH SI
  73.             PUSHF
  74.            
  75.             MOV AX, 0000H
  76.             MOV CX, 0000H
  77. CLOOP1:     MOV AX, [SI]            ;Loads character currently pointed to into AX
  78.             CMP AL, '9'
  79.             JNLE XCLOOP1        ;Exits loop if character is not less or equal to '9'
  80.             CMP AL, '0'
  81.             JNGE XCLOOP1        ;Exits loop if character is not greater or equal to '0'
  82.             INC CL          ;Else, Keeps count of the number of characters being processed
  83.             INC SI          ;Moves pointer to the next character
  84.             SUB AL, 30H     ;Converts character to digit
  85.             PUSH AX         ;Pushes digit to stack
  86.             JMP CLOOP1      ;continues loop
  87.  
  88. XCLOOP1:    MOV BX, 0001H
  89.             MOV DX, 0000H
  90. CLOOP2:     CMP CL, 0           ;Checks number of characters processed
  91.             JE XCLOOP2      ;Exits loop if no character was processed i.e CL = 0
  92.             POP AX          ;Else, pops digit from stack to AX
  93.             MUL BL          ;Multiplies digit with it's PLACEMENT-VALUE. RECALL: unit = 1, tens = 10, hundreds = 100 ...
  94.             ADD DX, AX      ;Adds (digit * PLACEMENT-VALUE)s together. RECALL: 234 = (4 * 1) + (3 * 10) + (2 * 100) where 1,10,100 represent their PLACEMENT-VALUES
  95.             MOV AX, 10             
  96.             MUL BL          ;Multiplies current PLACEMENT-VALUE by 10 to advance to the next PLACEMENT-VALUE.
  97.             XCHG AX, BX     ;Puts result from multiplication back to BX
  98.             DEC CX          ;Reduces count
  99.             JMP CLOOP2      ;Loop continues
  100.            
  101. XCLOOP2:    MOV AX, DX          ;Moves converted value to AX
  102.            
  103.             POPF
  104.             POP SI
  105.             POP DX
  106.             POP CX
  107.             POP BX
  108.             RET
  109. CHAR2INTR ENDP
  110. ;End of Subroutine CHAR2INTR
  111.  
  112. displayReal PROC
  113.             PUSH AX
  114.             PUSH BX
  115.             PUSH CX
  116.             PUSH DX
  117.             PUSHF
  118.             MOV BX, 10      ;Initializes divisor
  119.             MOV DX, 0000H          
  120.             MOV CX, 0000H                      
  121. .tryloop3:  MOV DX, 0000H          
  122.             DIV BX          ;Divides AX by 10
  123.             PUSH DX         ;Pushes DX(remainder) to stack
  124.             INC CX          ;Increments counter to track the number of digits
  125.             CMP AX, 0       ;Checks if there is still something in AX to divide
  126.             JNE .tryloop3       ;Jumps if AX is not zero
  127.            
  128.             SUB CX, 2       ;Moves back two decimal places
  129.            
  130. .tryloop4:  POP DX              ;Pops from stack to DX
  131.             ADD DX, 30H     ;Converts to it's ASCII equivalent
  132.             MOV AH, 02H                        
  133.             INT 21H         ;calls DOS to display character
  134.             LOOP .tryloop4      ;Loops till CX equals zero
  135.            
  136.             MOV DL, '.'            
  137.             MOV AH, 02H                                
  138.             INT 21H         ;Places a dot
  139.            
  140.             MOV CX, 0002H          
  141. .tryloop5:  POP DX              ;Pops from stack to DX
  142.             ADD DX, 30H     ;Converts to it's ASCII equivalent
  143.             MOV AH, 02H                                        
  144.             INT 21H
  145.             LOOP .tryloop5      ;Loops to display the decimal parts            
  146.             POPF
  147.             POP DX
  148.             POP CX
  149.             POP BX
  150.             POP AX
  151.             RET         ;returns control           
  152. displayReal ENDP
  153.  
  154. END start
  155. ;                               \|||/                                            
  156. ;          .-.________          (o o)                        ________.-.                    
  157. ;     ----/ \_)_______)  +-oooO--(_)---------------------+  (_______(_/ \----              
  158. ;        (    ()___)     | Get more Codes At:            |    (___()     )                  
  159. ;             ()__)      |                               |     (__()                        
  160. ;     ----\___()_)       | www.showmecodes.blogspot.com  |      (_()___/----                
  161. ;                        +-----------------------Ooo-----+                                  
  162. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement