Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; ex3.asm
- .MODEL SMALL
- .STACK 100h
- .DATA
- NumInsertion DB 'Please enter number: ','$'
- perfectstr DB 13,10,'Perfect number!','$'
- Not_perfectstr DB 13,10,'Not perfect number!','$'
- flag DB 0
- number DD 0
- ten DD 10
- two DD 2
- maxDer DD ?
- DerSum DD 0
- .CODE
- .386
- ;procedure 1 "getPNum":
- getPNum PROC NEAR
- ;PUSHing register values in order to save them
- PUSH EAX
- PUSH EDX
- ;set print option and print Character Insertion prompt
- MOV AH,9
- MOV DX,OFFSET NumInsertion
- INT 21h
- XOR CX, CX
- InsertionLoop:
- MOV EAX, 0
- ;set get character option and put it into temp
- MOV AH,1
- INT 21h
- ;compare the char inserted to ENTER, if equal end the loop
- MOV AH, 0
- CMP AL, 13
- JE EndLoop
- SUB AL, '0'
- ADD EAX, number
- MUL ten
- MOV number, EAX
- JMP InsertionLoop
- EndLoop:
- MOV EDX, 0
- MOV EAX, number
- DIV ten
- MOV number, EAX
- ;POPing stacked values back to registers
- POP EDX
- POP EAX
- ;Ending procedure
- RET
- getPNum ENDP
- ;procedure 2 "PNum":
- PNum PROC NEAR
- PUSH EAX
- PUSH EDX
- PUSH ECX
- ;initiate ECX as a counter
- MOV ECX, 0
- ;We can decrease the number of iterations by 2 if we set the maximun derative possible to number/2, there is not a number that divides by more than his half.
- MOV EAX, number
- MOV EDX, 0
- DIV two
- MOV maxDer, EAX
- LoopDerative:
- INC ECX
- MOV EAX, number
- MOV EDX, 0
- ;check the remain of the devition number/ECX
- DIV ECX
- CMP EDX, 0
- JNE dontCountDer
- ADD DerSum, ECX
- ;compare ECX and the max derative we had set in order to finish the loop
- dontCountDer:
- CMP ECX, maxDer
- JNE LoopDerative
- MOV EAX, DerSum
- CMP EAX, number
- JNE EndProc
- MOV flag, 1
- EndProc:
- POP ECX
- POP EDX
- POP EAX
- RET
- PNum ENDP
- ProgStart:
- MOV AX,@DATA
- MOV DS,AX
- ;Call procedure "getPNum" in order to receive the input from the user
- MOV DX, OFFSET number
- CALL getPNum
- ;Call procedure "PNum" in order to check if the number is perfect
- CALL PNum
- CMP flag, 0
- JE DispNotPerfect
- DispPerfect: ;set print option and print perfectstr
- MOV AH,9
- MOV DX,OFFSET perfectstr
- INT 21h
- JMP EndProgram
- DispNotPerfect: ;set print option and print Not_perfectstr
- MOV AH,9
- MOV DX,OFFSET Not_perfectstr
- INT 21h
- EndProgram: ; Set terminate option and return to DOS
- MOV AH,4Ch
- INT 21h
- END ProgStart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement