Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PRESERVE8
- AREA MyCode, CODE, READONLY
- EXPORT calc
- calc
- import my_fprint
- import my_print ;debug
- import my_getchar
- import my_putchar
- MOV r3,#0xA ;constant
- LDR r0,=cal
- BL my_fprint
- get_new
- MOV r0,#0x0A
- BL my_putchar ;outputs line feed between new things
- MOV r4,#0 ;reinitilise the saved number register
- BL my_getchar
- ;next_dig
- PUSH{r0}
- BL my_putchar
- POP{r1}
- ;num handling
- CMP r1,#0x39 ;ie temp r0-0x39
- BGT equals ;assume '=' only other larger value
- CMP r1,#0x30 ;ie temp r0 -0x30
- BLT op ;assume operators only smaller values
- AsciiToInt
- MOV r2,#0 ;store result here
- ;Argument needs to be compiled of get chars
- LDR r4,=argument ;load start address for argument
- ;LAB SLIDES
- NextAscii
- LDRB r3,[r4],#1 ;
- CMP r3,#0x0D
- BEQ get_new
- AND r3,#0x30 ;separate the decimal digit from ASCII character
- ADD r2,r3,r2
- LSL r5,r2,#3 ;multiply by 8, r5 = r2*8
- ADD r2,r5,r2, LSL #1 ;here we have r2 = r5 + r2*2
- B NextAscii
- done
- ;OLD CODE
- ; B asc2int
- ; ;SUB r1,r1,#0x30 ;convert to int
- ; MLA r4,r4,r3,r1 ;r4 = r4*10 + r0
- ; PUSH {r4}
- ; BL my_getchar
- ; MOV r1,r0
- ; CMP r1,#0x0D ;checks if multiple digit number
- ; BNE next_dig ;no CR at end therefore must be a digit
- ; BL my_putchar ;beginning of line
- ; B get_new ;end of number
- /*OPERATIONS*/
- op
- CMP r1,#"+"
- BEQ plus
- CMP r1,#"-"
- BEQ minus
- CMP r1,#"/"
- BEQ div
- CMP r1,#"*"
- BEQ mult
- plus
- BL my_getchar
- CMP r0,#0x0D ;verifies that the next charachter is an enter
- BNE error ;if not then the format is incorrect
- BL my_putchar ;beginning of line
- POP {r5}
- POP {r6}
- ADD r5,r6,r5 ;add arguments
- PUSH {r5}
- B get_new ;return from subroutine
- minus
- BL my_getchar
- CMP r0,#0x0D ;verifies that the next charachter is an enter
- BNE error ;if not then the format is incorrect
- BL my_putchar ;beginning of line
- POP {r5}
- POP {r6}
- SUB r5,r6,r5 ;add arguments ;old number - new number
- PUSH {r5}
- B get_new ;return from subroutine
- div
- BL my_getchar
- CMP r0,#0x0D ;verifies that the next charachter is an enter
- BNE error ;if not then the format is incorrect
- BL my_putchar ;beginning of line
- POP {r5}
- POP {r6}
- CMP r5,#0
- BEQ error ;divide by 0 error
- UDIV r5,r6,r5 ;divide arguments ;old number/new number; r6/r5
- PUSH {r5}
- B get_new ;return from subroutine
- mult
- BL my_getchar
- CMP r0,#0x0D ;verifies that the next charachter is an enter
- BNE error ;if not then the format is incorrect
- BL my_putchar ;beginning of line
- POP {r5}
- POP {r6}
- MUL r5,r6,r5 ;multiply arguments
- PUSH {r5}
- B get_new ;return from subroutine
- equals
- BL my_getchar
- CMP r0,#0x0D ;verifies that the next charachter is an enter
- BNE error ;if not then the format is incorrect
- BL my_putchar
- MOV r0,#0x0A ;LF
- BL my_putchar ;outputs line feed between new things
- POP {r1}
- MOV r2,#0 ;counter
- dec2asc
- ADD r2,r2,#1 ;increment counter
- CMP r1,r3 ;temp r1 - 10
- BLT output ;if no more digits jump
- UDIV r4,r1,r3 ;r4 = r1/10
- MLS r5,r4,r3,r1 ;r5 = r1- r4*10
- ;r5 = r1%10 and r4 = r1/10
- PUSH {r5} ;push LSB onto stack
- MOV r1,r4 ;continue with remaining digits
- B dec2asc
- output
- SUB r2,r2,#1 ;decrement counter
- ADD r0,r1,#0x30 ;MSB integer to ascii
- BL my_putchar ;outputs an integer
- POP {r1}
- CMP r2,#0
- BGT output
- B stop
- error
- LDR r0,=err
- BL my_fprint
- B stop
- /*END OPERATIONS*/
- stop B stop
- ALIGN ;aligns code (but also data) to a memory boundary
- AREA MyData, DATA, READWRITE
- ;strings
- err DCB 10,13,"An error occured in processing",0,10,13 ;NULL;LF;CR decimal
- cal DCB 10,10,13,"NEW CALCULATOR",0,13 ;NULL;LF;CR decimal
- y DCB "point",0;for debug
- ;zero DCB "cannot divide by zero",0
- ;inv DCB "invalid input formatting",0
- END ;designates end of source line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement