Advertisement
yosikadoshi

LAB7NEW

May 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; ex3.asm
  2.  
  3.     .MODEL SMALL
  4.    .STACK 100h
  5.    .DATA
  6. NumInsertion    DB 'Please enter number: ','$'
  7. perfectstr      DB 13,10,'Perfect number!','$'
  8. Not_perfectstr  DB 13,10,'Not perfect number!','$'
  9. flag            DB 0
  10. number          DD 0
  11. ten             DD 10
  12. two             DD 2
  13. maxDer          DD ?
  14. DerSum          DD 0
  15.  
  16.      .CODE
  17. .386
  18. ;procedure 1 "getPNum":
  19. getPNum  PROC NEAR
  20.     ;PUSHing register values in order to save them
  21.      PUSH EAX
  22.      PUSH EDX
  23.      
  24.     ;set print option and print Character Insertion prompt
  25.      MOV AH,9
  26.      MOV DX,OFFSET NumInsertion
  27.      INT 21h
  28.      XOR CX, CX
  29. InsertionLoop:   
  30.      MOV EAX, 0
  31.     ;set get character option and put it into temp
  32.      MOV AH,1
  33.      INT 21h
  34.     ;compare the char inserted to ENTER, if equal end the loop
  35.      MOV AH, 0
  36.      CMP AL, 13
  37.      JE EndLoop
  38.      SUB AL, '0'
  39.      ADD EAX, number
  40.      MUL ten
  41.      MOV number, EAX
  42.      JMP InsertionLoop
  43.      
  44. EndLoop:
  45.      MOV EDX, 0
  46.      MOV EAX, number
  47.      DIV ten
  48.      MOV number, EAX
  49.      
  50.     ;POPing stacked values back to registers     
  51.      POP EDX
  52.      POP EAX
  53.     ;Ending procedure
  54.      RET
  55.      getPNum  ENDP
  56.  
  57.      
  58. ;procedure 2 "PNum":
  59. PNum PROC NEAR
  60.      PUSH EAX
  61.      PUSH EDX
  62.      PUSH ECX
  63.     ;initiate ECX as a counter
  64.      MOV ECX, 0
  65.     ;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.
  66.      MOV EAX, number
  67.      MOV EDX, 0
  68.      DIV two
  69.      MOV maxDer, EAX
  70.  
  71. LoopDerative:
  72.      INC ECX
  73.      MOV EAX, number
  74.      MOV EDX, 0
  75.      ;check the remain of the devition number/ECX
  76.      DIV ECX
  77.      CMP EDX, 0
  78.      JNE dontCountDer
  79.      ADD DerSum, ECX
  80.     ;compare ECX and the max derative we had set in order to finish the loop
  81. dontCountDer:
  82.      CMP ECX, maxDer
  83.      JNE LoopDerative
  84.      
  85.      MOV EAX, DerSum
  86.      CMP EAX, number
  87.      JNE EndProc
  88.      MOV flag, 1
  89. EndProc:
  90.      POP ECX     
  91.      POP EDX
  92.      POP EAX
  93.      RET
  94.      PNum ENDP
  95.  
  96.  
  97. ProgStart:
  98.      MOV AX,@DATA
  99.      MOV DS,AX
  100. ;Call procedure "getPNum" in order to receive the input from the user
  101.      MOV DX, OFFSET number
  102.      CALL getPNum
  103. ;Call procedure "PNum" in order to check if the number is perfect    
  104.      CALL PNum
  105.      CMP flag, 0
  106.      JE DispNotPerfect
  107. DispPerfect:        ;set print option and print perfectstr
  108.      MOV AH,9
  109.      MOV DX,OFFSET perfectstr
  110.      INT 21h
  111.      JMP EndProgram
  112.      
  113. DispNotPerfect:     ;set print option and print Not_perfectstr
  114.      MOV AH,9
  115.      MOV DX,OFFSET Not_perfectstr
  116.      INT 21h
  117.      
  118. EndProgram:         ; Set terminate option and return to DOS
  119.      MOV AH,4Ch
  120.      INT 21h
  121.     END ProgStart
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement