Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AREA DisplayResult, CODE, READONLY
- IMPORT main
- IMPORT getkey
- IMPORT sendchar
- EXPORT start
- PRESERVE8
- start
- LDR R4, =0x0 ; clear register First number enetered by user
- LDR R6, =0x0 ; clear register Second number entered by user
- LDR R10, =10 ;
- LDR R12, =0 ; clear register Set to 0xFFFFFFFF if invalid input
- readFirstNum
- BL getkey ; read key from console
- CMP R0, #0x0D ; while (key != CR)
- BEQ endRead ; {
- BL sendchar ; echo key back to console
- CMP R0, #'+' ; while (key != +)
- BEQ isAdd ;
- CMP R0, #'-' ; && while (key != -)
- BEQ isSub ;
- CMP R0, #'*' ; && while (key != *)
- BEQ isMul ;
- CMP R0, #'/' ; && while (key != /)
- BEQ isDiv ;
- CMP R0, #'%' ; && while (key != %)
- BEQ isMod ;
- CMP R0, #'!' ; && while (key != !)
- BEQ isFac ; {
- SUB R0, R0, #0x30 ; convert from ASCII to actual number
- MUL R4, R10, R4 ; move numbers one step to the left (from decimal point)
- ADD R4, R4, R0 ; add next number entered by the user to the previous
- B readFirstNum ; }
- isAdd ;
- LDR R7, ='+' ; store + operator in R7
- B readSecondNum ;
- isSub ;
- LDR R7, ='-' ; store - operator in R7
- B readSecondNum ;
- isMul ;
- LDR R7, ='*' ; store * operator in R7
- B readSecondNum ;
- isDiv
- LDR R7, ='/' ; store / operator in R7
- B readSecondNum ;
- isMod
- LDR R7, ='%' ; store % operator in R7
- B readSecondNum ;
- isFac
- LDR R7, ='!' ; store ! operator in R7
- CMP R0, #0x0D ; while (key != CR)
- BEQ endRead ;
- readSecondNum
- BL getkey ; read key from console
- CMP R0, #0x0D ; while (key != CR)
- BEQ endRead ; {
- BL sendchar ; echo key back to console
- SUB R0, R0, #0x30 ; convert from ASCII to actual number
- MUL R6, R10, R6 ; move numbers one step to the left (from decimal point)
- ADD R6, R6, R0 ; add next number entered by the user to the previous
- B readSecondNum ; }
- endRead
- CMP R7, #'+'
- BEQ addCalc
- CMP R7, #'-'
- BEQ subCalc
- CMP R7, #'*'
- BEQ mulCalc
- CMP R7, #'/'
- BEQ divCalc
- CMP R7, #'%'
- BEQ modCalc
- CMP R7, #'!'
- BEQ facCalc
- B invalidInput ;
- addCalc
- ADD R5, R4, R6
- B endCalc
- subCalc
- CMP R4, R6
- BLO invalidInput
- SUB R5, R4, R6
- B endCalc
- mulCalc
- MUL R5, R4, R6
- B endCalc
- divCalc
- LDR R0, =0 ; quotient First number entered by user = a
- MOV R1, R4 ; remainder = a Second number entered by user = b
- zeroCheck
- CMP R6, #0
- BEQ invalidInput
- whRemGTb
- CMP R1, R6 ; while (remainder >=b)
- BLO endDivCalc ;
- ADD R0, R0, #1 ; quotient = quotient + 1
- SUB R1, R1, R6 ; remainder = remainder - b
- B whRemGTb ;
- endDivCalc
- MOV R5, R0
- MOV R8, R1
- B endCalc
- modCalc
- LDR R0, =0 ; quotient First number entered by user = a
- MOV R1, R4 ; remainder = a Second number entered by user = b
- zero
- CMP R6, #0
- BEQ invalidInput
- whRemGTQuo
- CMP R1, R6 ; while (remainder >=b)
- BLO endModCalc ;
- ADD R0, R0, #1 ; quotient = quotient + 1
- SUB R1, R1, R6 ; remainder = remainder - b
- B whRemGTQuo ;
- endModCalc
- MOV R5, R1
- B endCalc
- facCalc
- LDR R1, =1 ; result = 1
- CMP R4, #13
- BHS invalidInput
- facMul
- CMP R4, #1 ; while (number > 1)
- BLS endFacCalc ; {
- MUL R1, R4, R1 ; result = number * result
- SUB R4, R4, #1 ; number = number - 1
- B facMul ; }
- endFacCalc
- MOV R5, R1 ; move result to R5
- B endCalc
- invalidInput ; ****************************************
- LDR R12, =0XFFFFFFFF ; Used to indicate invalid input
- B invalidInOutput
- endCalc
- LDR R0, ='=' ; print =
- BL sendchar
- LDR R11, =1000000000
- CMP R5, R11
- BHS billion
- LDR R11, =100000000
- CMP R5, R11
- BHS hundredMillion
- LDR R11, =10000000
- CMP R5, R11
- BHS tenMillion
- LDR R11, =1000000
- CMP R5, R11
- BHS million
- LDR R11, =100000
- CMP R5, R11
- BHS hundredThousand
- LDR R11, =10000
- CMP R5, R11
- BHS tenThousand
- LDR R11, =1000
- CMP R5, R11
- BHS thousand
- CMP R5, #100
- BHS hundred
- CMP R5, #10
- BHS ten
- B singleDigit ; *********************************
- billion
- LDR R9, =0 ; quotient
- MOV R10, R5 ; remainder = a
- whBillion
- CMP R10, R11 ; while (remainder >=billion)
- BLS endBillion ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whBillion ;
- endBillion
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- ;B stop
- ;divByTen
- ; LDR R0, =0 ; quotient
- ; MOV R1, R11 ; remainder = a
- ;zeroCheck3
- ; CMP R1, #0
- ; BEQ endDivTen
- ;whGTzero
- ; CMP R1, #10 ; while (remainder >=b)
- ; BLO stop ;
- ; ADD R0, R0, #1 ; quotient = quotient + 1
- ; SUB R1, R1, #10 ; remainder = remainder - b
- ; B whGTzero ;
- ;endDivTen
- ; MOV R11, R0
- ; B billion
- hundredMillion
- LDR R9, =0 ; quotient
- LDR R11, =100000000
- MOV R10, R5 ; remainder = a
- whHundredMillion
- CMP R10, R11 ; while (remainder >=hundredMillion)
- BLS endHundredMillion ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whHundredMillion ;
- endHundredMillion
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- tenMillion
- LDR R9, =0 ; quotient
- LDR R11, =10000000
- MOV R10, R5 ; remainder = a
- whTenMillion
- CMP R10, R11 ; while (remainder >=tenMillion)
- BLS endTenMillion ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whTenMillion ;
- endTenMillion
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- million
- LDR R9, =0 ; quotient
- LDR R11, =1000000
- MOV R10, R5 ; remainder = a
- whMillion
- CMP R10, R11 ; while (remainder >=million)
- BLS endMillion ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whMillion ;
- endMillion
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- hundredThousand
- LDR R9, =0 ; quotient
- LDR R11, =100000
- MOV R10, R5 ; remainder = a
- whHundredThousand
- CMP R10, R11 ; while (remainder >=hundredThousand)
- BLS endHundredThousand ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whHundredThousand ;
- endHundredThousand
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- tenThousand
- LDR R9, =0 ; quotient
- LDR R11, =10000
- MOV R10, R5 ; remainder = a
- whTenThousand
- CMP R10, R11 ; while (remainder >=tenThousand)
- BLS endTenThousand ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whTenThousand ;
- endTenThousand
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- thousand
- LDR R9, =0 ; quotient
- LDR R11, =1000
- MOV R10, R5 ; remainder = a
- whThousand
- CMP R10, R11 ; while (remainder >=thousand)
- BLS endThousand ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whThousand ;
- endThousand
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- hundred
- LDR R9, =0 ; quotient
- LDR R11, =100
- MOV R10, R5 ; remainder = a
- whHundred
- CMP R10, R11 ; while (remainder >=hundred)
- BLS endHundred ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whHundred ;
- endHundred
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- ten
- LDR R9, =0 ; quotient
- LDR R11, =10
- MOV R10, R5 ; remainder = a
- whTen
- CMP R10, R11 ; while (remainder >=ten)
- BLS endTen ;
- ADD R9, R9, #1 ; quotient = quotient + 1
- SUB R10, R10, R11 ; remainder = remainder - b
- B whTen ;
- endTen
- MOV R0, R9
- ADD R0, R0, #'0'
- BL sendchar
- MOV R5, R10
- singleDigit
- MOV R0, R5
- ADD R0, R0, #'0'
- BL sendchar
- divRemainder
- CMP R7, #'/'
- BNE stop
- LDR R0, ='r'
- BL sendchar
- MOV R0, R8
- ADD R0, R0, #'0'
- BL sendchar
- B stop
- invalidInOutput
- LDR R9, =invalidInStr ; invalidInStr ; address = invalid input string
- LDRB R0, [R9] ;
- whInvStr
- CMP R0, #0 ;
- BEQ endWhInv
- BL sendchar
- ADD R9, R9, #1
- LDRB R0, [R9]
- B whInvStr
- endWhInv
- stop B stop
- AREA invalidInOutputData, DATA, READWRITE
- invalidInStr
- DCB " = Invalid Input! Please try again. ",0 ; Null terminated invalid input string
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement