Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;AUTHOR: ANUEBUNWA VICTOR OGECHUKWU
- ;PROGRAM TO CALCULATE AREA AND CIRCUMFERENCE OF A CIRCLE IN 8086 ASSEMBLY
- ;ASSEMBLER: MASM
- .MODEL SMALL
- .STACK
- .DATA
- PIx100 EQU 314 ;PI(approximated to 2dp) * 100
- MSG1 DB "Enter radius: ",'$'
- MSG2 DB 0AH,0DH,"The area of the circle is: ",'$'
- MSG3 DB 0AH,0DH,"and it's circumference is: ",'$'
- AREA DW ?
- CIRCUMFERENCE DW ?
- VALUE DW ?
- .CODE
- start: MOV AX, @data
- MOV DS, AX
- MOV AH, 09H ;Print string function
- LEA DX, MSG1
- INT 21H
- MOV AH, 3FH ;Input string function
- LEA DX, VALUE
- INT 21H
- LEA SI, VALUE
- CALL CHAR2INTR ;Converts collected characters to an integer and stores result in AX
- ;Calculating Area
- MOV VALUE, AX
- MUL VALUE ;raduis * radius
- MOV BX, PIx100
- MUL BX ;(radius * radius) * (PI*100)
- MOV BX, 100 ;Subroutine displayReal requires that you multiply value by 100 before division (to move value dp up 2 places)
- MUL BX ;i.e 4.230 * 100 = 423.0 (Moving dp up 2 places)
- ;dp will be placed at its appropriate position during display(in subroutine displayReal)
- MOV BX, 100
- DIV BX ;(radius * radius) * (PI*100)/100 = (radius * radius) * PI
- MOV AREA, AX ;Stores result
- ;Calculating Circumference
- MOV AX, VALUE
- MOV BX, 2
- MUL BX ;raduis * 2
- MOV BX, PIx100
- MUL BX ;(radius * 2) * (PI*100)
- MOV BX, 100
- MUL BX ;Moving dp up 2 places
- MOV BX, 100
- DIV BX ;(radius * 2) * (PI*100)/100 = (radius * 2) * PI
- MOV CIRCUMFERENCE, AX ;Stores result
- MOV AH, 09H
- LEA DX, MSG2
- INT 21H
- MOV AX, AREA
- CALL DISPLAYREAL ;Displays Area
- MOV AH, 09H
- LEA DX, MSG3
- INT 21H
- MOV AX, CIRCUMFERENCE
- CALL DISPLAYREAL ;Displays Circumference
- MOV AH, 4CH
- INT 21H ;Program terminates
- CHAR2INTR PROC
- PUSH BX
- PUSH CX
- PUSH DX
- PUSH SI
- PUSHF
- MOV AX, 0000H
- MOV CX, 0000H
- CLOOP1: MOV AX, [SI] ;Loads character currently pointed to into AX
- CMP AL, '9'
- JNLE XCLOOP1 ;Exits loop if character is not less or equal to '9'
- CMP AL, '0'
- JNGE XCLOOP1 ;Exits loop if character is not greater or equal to '0'
- INC CL ;Else, Keeps count of the number of characters being processed
- INC SI ;Moves pointer to the next character
- SUB AL, 30H ;Converts character to digit
- PUSH AX ;Pushes digit to stack
- JMP CLOOP1 ;continues loop
- XCLOOP1: MOV BX, 0001H
- MOV DX, 0000H
- CLOOP2: CMP CL, 0 ;Checks number of characters processed
- JE XCLOOP2 ;Exits loop if no character was processed i.e CL = 0
- POP AX ;Else, pops digit from stack to AX
- MUL BL ;Multiplies digit with it's PLACEMENT-VALUE. RECALL: unit = 1, tens = 10, hundreds = 100 ...
- 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
- MOV AX, 10
- MUL BL ;Multiplies current PLACEMENT-VALUE by 10 to advance to the next PLACEMENT-VALUE.
- XCHG AX, BX ;Puts result from multiplication back to BX
- DEC CX ;Reduces count
- JMP CLOOP2 ;Loop continues
- XCLOOP2: MOV AX, DX ;Moves converted value to AX
- POPF
- POP SI
- POP DX
- POP CX
- POP BX
- RET
- CHAR2INTR ENDP
- ;End of Subroutine CHAR2INTR
- displayReal PROC
- PUSH AX
- PUSH BX
- PUSH CX
- PUSH DX
- PUSHF
- MOV BX, 10 ;Initializes divisor
- MOV DX, 0000H
- MOV CX, 0000H
- .tryloop3: MOV DX, 0000H
- DIV BX ;Divides AX by 10
- PUSH DX ;Pushes DX(remainder) to stack
- INC CX ;Increments counter to track the number of digits
- CMP AX, 0 ;Checks if there is still something in AX to divide
- JNE .tryloop3 ;Jumps if AX is not zero
- SUB CX, 2 ;Moves back two decimal places
- .tryloop4: POP DX ;Pops from stack to DX
- ADD DX, 30H ;Converts to it's ASCII equivalent
- MOV AH, 02H
- INT 21H ;calls DOS to display character
- LOOP .tryloop4 ;Loops till CX equals zero
- MOV DL, '.'
- MOV AH, 02H
- INT 21H ;Places a dot
- MOV CX, 0002H
- .tryloop5: POP DX ;Pops from stack to DX
- ADD DX, 30H ;Converts to it's ASCII equivalent
- MOV AH, 02H
- INT 21H
- LOOP .tryloop5 ;Loops to display the decimal parts
- POPF
- POP DX
- POP CX
- POP BX
- POP AX
- RET ;returns control
- displayReal ENDP
- END start
- ; \|||/
- ; .-.________ (o o) ________.-.
- ; ----/ \_)_______) +-oooO--(_)---------------------+ (_______(_/ \----
- ; ( ()___) | Get more Codes At: | (___() )
- ; ()__) | | (__()
- ; ----\___()_) | www.showmecodes.blogspot.com | (_()___/----
- ; +-----------------------Ooo-----+
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement