Advertisement
myersjo

DisplayResult2

Nov 20th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 8.12 KB | None | 0 0
  1.     AREA    DisplayResult, CODE, READONLY
  2.     IMPORT  main
  3.     IMPORT  getkey
  4.     IMPORT  sendchar
  5.     EXPORT  start
  6.     PRESERVE8
  7.  
  8. start
  9.     LDR R4, =0x0    ; clear register        First number enetered by user
  10.     LDR R6, =0x0    ; clear register        Second number entered by user
  11.     LDR R10, =10    ;
  12.     LDR R12, =0     ; clear register        Set to 0xFFFFFFFF if invalid input
  13.    
  14. readFirstNum
  15.     BL  getkey      ; read key from console
  16.     CMP R0, #0x0D   ; while (key != CR)
  17.     BEQ endRead     ; {
  18.     BL  sendchar    ;   echo key back to console
  19.    
  20.     CMP R0, #'+'    ; while (key != +)
  21.     BEQ isAdd       ;
  22.     CMP R0, #'-'    ; && while (key != -)
  23.     BEQ isSub       ;
  24.     CMP R0, #'*'    ; && while (key != *)
  25.     BEQ isMul       ;
  26.     CMP R0, #'/'    ; && while (key != /)
  27.     BEQ isDiv       ;
  28.     CMP R0, #'%'    ; && while (key != %)
  29.     BEQ isMod       ;
  30.     CMP R0, #'!'    ; && while (key != !)
  31.     BEQ isFac       ; {
  32.    
  33.     SUB R0, R0, #0x30 ; convert from ASCII to actual number
  34.     MUL R4, R10, R4 ; move numbers one step to the left (from decimal point)
  35.     ADD R4, R4, R0  ; add next number entered by the user to the previous
  36.  
  37.     B   readFirstNum ; }
  38.  
  39. isAdd               ;
  40.     LDR R7, ='+'    ; store + operator in R7
  41.     B readSecondNum ;
  42.    
  43. isSub               ;
  44.     LDR R7, ='-'    ; store - operator in R7
  45.     B readSecondNum ;
  46.    
  47. isMul               ;
  48.     LDR R7, ='*'    ; store * operator in R7
  49.     B readSecondNum ;
  50.    
  51. isDiv
  52.     LDR R7, ='/'    ; store / operator in R7
  53.     B readSecondNum ;
  54.  
  55. isMod
  56.     LDR R7, ='%'    ; store % operator in R7
  57.     B readSecondNum ;
  58.  
  59. isFac
  60.     LDR R7, ='!'    ; store ! operator in R7
  61.     CMP R0, #0x0D   ; while (key != CR)
  62.     BEQ endRead     ;
  63.    
  64. readSecondNum
  65.     BL  getkey      ; read key from console
  66.     CMP R0, #0x0D   ; while (key != CR)
  67.     BEQ endRead     ; {
  68.     BL  sendchar    ;   echo key back to console
  69.    
  70.     SUB R0, R0, #0x30 ; convert from ASCII to actual number
  71.     MUL R6, R10, R6 ; move numbers one step to the left (from decimal point)
  72.     ADD R6, R6, R0  ; add next number entered by the user to the previous
  73.  
  74.     B   readSecondNum       ; }
  75.    
  76. endRead
  77.  
  78.     CMP R7, #'+'
  79.     BEQ addCalc
  80.     CMP R7, #'-'
  81.     BEQ subCalc
  82.     CMP R7, #'*'
  83.     BEQ mulCalc
  84.     CMP R7, #'/'
  85.     BEQ divCalc
  86.     CMP R7, #'%'
  87.     BEQ modCalc
  88.     CMP R7, #'!'
  89.     BEQ facCalc
  90.     B invalidInput      ;
  91.    
  92. addCalc
  93.     ADD R5, R4, R6
  94.     B endCalc
  95.    
  96. subCalc
  97.     CMP R4, R6
  98.     BLO invalidInput
  99.     SUB R5, R4, R6
  100.     B endCalc
  101.  
  102. mulCalc
  103.     MUL R5, R4, R6
  104.     B endCalc
  105.    
  106.    
  107. divCalc
  108.     LDR R0, =0      ; quotient          First number entered by user = a
  109.     MOV R1, R4      ; remainder = a     Second number entered by user = b
  110. zeroCheck
  111.     CMP R6, #0
  112.     BEQ invalidInput  
  113. whRemGTb
  114.     CMP R1, R6      ; while (remainder >=b)
  115.     BLO endDivCalc  ;
  116.     ADD R0, R0, #1  ; quotient = quotient + 1
  117.     SUB R1, R1, R6  ; remainder = remainder - b
  118.     B   whRemGTb    ;      
  119. endDivCalc
  120.     MOV R5, R0
  121.     MOV R8, R1
  122.     B endCalc
  123.  
  124.  
  125. modCalc
  126.     LDR R0, =0      ; quotient          First number entered by user = a
  127.     MOV R1, R4      ; remainder = a     Second number entered by user = b
  128. zero
  129.     CMP R6, #0
  130.     BEQ invalidInput  
  131. whRemGTQuo
  132.     CMP R1, R6      ; while (remainder >=b)
  133.     BLO endModCalc  ;
  134.     ADD R0, R0, #1  ; quotient = quotient + 1
  135.     SUB R1, R1, R6  ; remainder = remainder - b
  136.     B   whRemGTQuo  ;      
  137. endModCalc
  138.     MOV R5, R1
  139.     B endCalc
  140.  
  141.  
  142. facCalc
  143.     LDR R1, =1      ; result = 1
  144.     CMP R4, #13
  145.     BHS invalidInput       
  146. facMul
  147.     CMP R4, #1      ; while (number > 1)
  148.     BLS endFacCalc  ; {
  149.     MUL R1, R4, R1  ; result = number * result
  150.     SUB R4, R4, #1  ; number = number - 1
  151.     B facMul        ; }
  152. endFacCalc
  153.     MOV R5, R1      ; move result to R5
  154.     B endCalc
  155.    
  156. invalidInput        ;                       ****************************************
  157.     LDR R12, =0XFFFFFFFF    ;   Used to indicate invalid input
  158.     B invalidInOutput
  159.        
  160. endCalc
  161.  
  162.     LDR R0, ='='    ; print =
  163.     BL sendchar
  164.    
  165.     LDR R11, =1000000000
  166.     CMP R5, R11
  167.     BHS billion
  168.     LDR R11, =100000000
  169.     CMP R5, R11
  170.     BHS hundredMillion
  171.     LDR R11, =10000000
  172.     CMP R5, R11
  173.     BHS tenMillion
  174.     LDR R11, =1000000
  175.     CMP R5, R11
  176.     BHS million
  177.     LDR R11, =100000
  178.     CMP R5, R11
  179.     BHS hundredThousand
  180.     LDR R11, =10000
  181.     CMP R5, R11
  182.     BHS tenThousand
  183.     LDR R11, =1000
  184.     CMP R5, R11
  185.     BHS thousand
  186.     CMP R5, #100
  187.     BHS hundred
  188.     CMP R5, #10
  189.     BHS ten
  190.     B singleDigit           ; *********************************
  191.    
  192. billion
  193.     LDR R9, =0      ; quotient         
  194.     MOV R10, R5     ; remainder = a    
  195. whBillion
  196.     CMP R10, R11            ; while (remainder >=billion)
  197.     BLS endBillion          ;
  198.     ADD R9, R9, #1          ; quotient = quotient + 1
  199.     SUB R10, R10, R11       ; remainder = remainder - b
  200.     B   whBillion       ;      
  201. endBillion
  202.     MOV R0, R9
  203.     ADD R0, R0, #'0'
  204.     BL sendchar
  205.     MOV R5, R10
  206.     ;B stop
  207.    
  208.    
  209. ;divByTen
  210. ;   LDR R0, =0      ; quotient         
  211. ;   MOV R1, R11     ; remainder = a    
  212. ;zeroCheck3
  213. ;   CMP R1, #0
  214. ;   BEQ endDivTen  
  215. ;whGTzero
  216. ;   CMP R1, #10     ; while (remainder >=b)
  217. ;   BLO stop    ;
  218. ;   ADD R0, R0, #1  ; quotient = quotient + 1
  219. ;   SUB R1, R1, #10 ; remainder = remainder - b
  220. ;   B   whGTzero    ;      
  221. ;endDivTen
  222. ;   MOV R11, R0
  223. ;   B billion
  224.    
  225.    
  226.    
  227. hundredMillion
  228.     LDR R9, =0      ; quotient         
  229.     LDR R11, =100000000
  230.     MOV R10, R5     ; remainder = a    
  231. whHundredMillion
  232.     CMP R10, R11            ; while (remainder >=hundredMillion)
  233.     BLS endHundredMillion   ;
  234.     ADD R9, R9, #1          ; quotient = quotient + 1
  235.     SUB R10, R10, R11       ; remainder = remainder - b
  236.     B   whHundredMillion        ;      
  237. endHundredMillion
  238.     MOV R0, R9
  239.     ADD R0, R0, #'0'
  240.     BL sendchar
  241.     MOV R5, R10
  242.    
  243. tenMillion
  244.     LDR R9, =0      ; quotient         
  245.     LDR R11, =10000000
  246.     MOV R10, R5     ; remainder = a    
  247. whTenMillion
  248.     CMP R10, R11            ; while (remainder >=tenMillion)
  249.     BLS endTenMillion   ;
  250.     ADD R9, R9, #1          ; quotient = quotient + 1
  251.     SUB R10, R10, R11       ; remainder = remainder - b
  252.     B   whTenMillion        ;      
  253. endTenMillion
  254.     MOV R0, R9
  255.     ADD R0, R0, #'0'
  256.     BL sendchar
  257.     MOV R5, R10
  258.    
  259.  
  260. million
  261.     LDR R9, =0      ; quotient         
  262.     LDR R11, =1000000
  263.     MOV R10, R5     ; remainder = a    
  264. whMillion
  265.     CMP R10, R11            ; while (remainder >=million)
  266.     BLS endMillion  ;
  267.     ADD R9, R9, #1          ; quotient = quotient + 1
  268.     SUB R10, R10, R11       ; remainder = remainder - b
  269.     B   whMillion       ;      
  270. endMillion
  271.     MOV R0, R9
  272.     ADD R0, R0, #'0'
  273.     BL sendchar
  274.     MOV R5, R10
  275.  
  276. hundredThousand
  277.     LDR R9, =0      ; quotient         
  278.     LDR R11, =100000
  279.     MOV R10, R5     ; remainder = a    
  280. whHundredThousand
  281.     CMP R10, R11            ; while (remainder >=hundredThousand)
  282.     BLS endHundredThousand  ;
  283.     ADD R9, R9, #1          ; quotient = quotient + 1
  284.     SUB R10, R10, R11       ; remainder = remainder - b
  285.     B   whHundredThousand   ;      
  286. endHundredThousand
  287.     MOV R0, R9
  288.     ADD R0, R0, #'0'
  289.     BL sendchar
  290.     MOV R5, R10
  291.  
  292. tenThousand
  293.     LDR R9, =0      ; quotient         
  294.     LDR R11, =10000
  295.     MOV R10, R5     ; remainder = a    
  296. whTenThousand
  297.     CMP R10, R11            ; while (remainder >=tenThousand)
  298.     BLS endTenThousand  ;
  299.     ADD R9, R9, #1          ; quotient = quotient + 1
  300.     SUB R10, R10, R11       ; remainder = remainder - b
  301.     B   whTenThousand   ;      
  302. endTenThousand
  303.     MOV R0, R9
  304.     ADD R0, R0, #'0'
  305.     BL sendchar
  306.     MOV R5, R10
  307.  
  308. thousand
  309.     LDR R9, =0      ; quotient         
  310.     LDR R11, =1000
  311.     MOV R10, R5     ; remainder = a    
  312. whThousand
  313.     CMP R10, R11            ; while (remainder >=thousand)
  314.     BLS endThousand ;
  315.     ADD R9, R9, #1          ; quotient = quotient + 1
  316.     SUB R10, R10, R11       ; remainder = remainder - b
  317.     B   whThousand  ;      
  318. endThousand
  319.     MOV R0, R9
  320.     ADD R0, R0, #'0'
  321.     BL sendchar
  322.     MOV R5, R10
  323.  
  324. hundred
  325.     LDR R9, =0              ; quotient         
  326.     LDR R11, =100
  327.     MOV R10, R5             ; remainder = a    
  328. whHundred
  329.     CMP R10, R11            ; while (remainder >=hundred)
  330.     BLS endHundred          ;
  331.     ADD R9, R9, #1          ; quotient = quotient + 1
  332.     SUB R10, R10, R11       ; remainder = remainder - b
  333.     B   whHundred           ;      
  334. endHundred
  335.     MOV R0, R9
  336.     ADD R0, R0, #'0'
  337.     BL sendchar
  338.     MOV R5, R10
  339.  
  340. ten
  341.     LDR R9, =0              ; quotient         
  342.     LDR R11, =10
  343.     MOV R10, R5             ; remainder = a    
  344. whTen
  345.     CMP R10, R11            ; while (remainder >=ten)
  346.     BLS endTen              ;
  347.     ADD R9, R9, #1          ; quotient = quotient + 1
  348.     SUB R10, R10, R11       ; remainder = remainder - b
  349.     B   whTen   ;      
  350. endTen
  351.     MOV R0, R9
  352.     ADD R0, R0, #'0'
  353.     BL sendchar
  354.     MOV R5, R10
  355.  
  356. singleDigit
  357.     MOV R0, R5
  358.     ADD R0, R0, #'0'
  359.     BL sendchar
  360.    
  361. divRemainder   
  362.     CMP R7, #'/'
  363.     BNE stop
  364.     LDR R0, ='r'
  365.     BL sendchar
  366.     MOV R0, R8
  367.     ADD R0, R0, #'0'
  368.     BL sendchar
  369.     B stop
  370.    
  371.  
  372.  
  373. invalidInOutput
  374.     LDR R9, =invalidInStr   ;   invalidInStr    ; address = invalid input string
  375.     LDRB R0, [R9]           ;
  376. whInvStr
  377.     CMP R0, #0              ;
  378.     BEQ endWhInv
  379.     BL sendchar
  380.     ADD R9, R9, #1
  381.     LDRB R0, [R9]
  382.     B whInvStr
  383. endWhInv
  384.    
  385.    
  386. stop    B   stop
  387.  
  388.  
  389.     AREA invalidInOutputData, DATA, READWRITE
  390.        
  391. invalidInStr   
  392.     DCB     " = Invalid Input! Please try again. ",0    ;   Null terminated invalid input string
  393.  
  394.     END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement