Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 3.74 KB | None | 0 0
  1.  
  2.     PRESERVE8
  3.     AREA MyCode, CODE, READONLY
  4.     EXPORT calc
  5.        
  6. calc
  7.     import my_fprint
  8.     import my_print ;debug
  9.     import my_getchar
  10.     import my_putchar
  11.     MOV r3,#0xA     ;constant
  12.     LDR r0,=cal
  13.     BL  my_fprint
  14.  
  15. get_new
  16.     MOV r0,#0x0A
  17.     BL  my_putchar  ;outputs line feed between new things
  18.     MOV r4,#0       ;reinitilise the saved number register
  19.     BL my_getchar
  20. ;next_dig
  21.     PUSH{r0}
  22.     BL my_putchar
  23.     POP{r1}
  24. ;num handling
  25.     CMP r1,#0x39        ;ie temp r0-0x39
  26.     BGT equals          ;assume '=' only other larger value
  27.     CMP r1,#0x30        ;ie temp r0 -0x30
  28.     BLT op              ;assume operators only smaller values
  29. AsciiToInt 
  30.     MOV r2,#0           ;store result here
  31.  
  32. ;Argument needs to be compiled of get chars
  33. LDR r4,=argument    ;load start address for argument
  34.  
  35. ;LAB SLIDES
  36. NextAscii
  37.     LDRB r3,[r4],#1 ;
  38.     CMP r3,#0x0D
  39.     BEQ get_new
  40.     AND r3,#0x30        ;separate the decimal digit from ASCII character
  41.     ADD r2,r3,r2
  42.     LSL r5,r2,#3        ;multiply by 8, r5 = r2*8
  43.     ADD r2,r5,r2, LSL #1    ;here we have r2 = r5 + r2*2
  44.     B   NextAscii
  45. done
  46. ;OLD CODE
  47.    
  48. ;   B   asc2int
  49. ;   ;SUB r1,r1,#0x30        ;convert to int
  50. ;   MLA r4,r4,r3,r1     ;r4 = r4*10 + r0
  51. ;   PUSH {r4}
  52. ;   BL  my_getchar
  53. ;   MOV r1,r0
  54. ;   CMP r1,#0x0D        ;checks if multiple digit number
  55. ;   BNE next_dig        ;no CR at end therefore must be a digit
  56. ;   BL  my_putchar      ;beginning of line
  57. ;   B   get_new         ;end of number
  58.  
  59.  
  60. /*OPERATIONS*/
  61. op
  62.     CMP r1,#"+"
  63.     BEQ plus
  64.    
  65.     CMP r1,#"-"
  66.     BEQ minus
  67.    
  68.     CMP r1,#"/"
  69.     BEQ div
  70.    
  71.     CMP r1,#"*"
  72.     BEQ mult
  73.    
  74. plus
  75.     BL my_getchar
  76.     CMP r0,#0x0D    ;verifies that the next charachter is an enter
  77.     BNE error   ;if not then the format is incorrect
  78.     BL  my_putchar  ;beginning of line
  79.     POP {r5}
  80.     POP {r6}
  81.     ADD r5,r6,r5    ;add arguments
  82.     PUSH {r5}
  83.     B   get_new ;return from subroutine
  84.    
  85. minus
  86.     BL my_getchar
  87.     CMP r0,#0x0D    ;verifies that the next charachter is an enter
  88.     BNE error   ;if not then the format is incorrect
  89.     BL  my_putchar  ;beginning of line
  90.     POP {r5}
  91.     POP {r6}
  92.     SUB r5,r6,r5    ;add arguments ;old number - new number
  93.     PUSH {r5}
  94.     B   get_new ;return from subroutine
  95.  
  96. div
  97.     BL my_getchar
  98.     CMP r0,#0x0D    ;verifies that the next charachter is an enter
  99.     BNE error   ;if not then the format is incorrect
  100.     BL  my_putchar  ;beginning of line
  101.     POP {r5}
  102.     POP {r6}
  103.     CMP r5,#0
  104.     BEQ error       ;divide by 0 error
  105.     UDIV r5,r6,r5   ;divide arguments   ;old number/new number; r6/r5
  106.     PUSH {r5}
  107.     B   get_new ;return from subroutine
  108.  
  109. mult
  110.     BL my_getchar
  111.     CMP r0,#0x0D    ;verifies that the next charachter is an enter
  112.     BNE error   ;if not then the format is incorrect
  113.     BL  my_putchar  ;beginning of line
  114.     POP {r5}
  115.     POP {r6}
  116.     MUL r5,r6,r5    ;multiply arguments
  117.     PUSH {r5}
  118.     B   get_new ;return from subroutine
  119.  
  120. equals
  121.     BL  my_getchar
  122.     CMP r0,#0x0D    ;verifies that the next charachter is an enter
  123.     BNE error   ;if not then the format is incorrect
  124.     BL  my_putchar
  125.     MOV r0,#0x0A    ;LF
  126.     BL  my_putchar  ;outputs line feed between new things
  127.     POP {r1}
  128.     MOV r2,#0   ;counter
  129.    
  130. dec2asc
  131.     ADD r2,r2,#1    ;increment counter
  132.     CMP r1,r3   ;temp r1 - 10
  133.     BLT output  ;if no more digits jump
  134.     UDIV r4,r1,r3   ;r4 = r1/10
  135.     MLS  r5,r4,r3,r1 ;r5 = r1- r4*10
  136.     ;r5 = r1%10 and r4 = r1/10
  137.     PUSH {r5}   ;push LSB onto stack
  138.     MOV r1,r4   ;continue with remaining digits
  139.     B   dec2asc
  140.  
  141. output
  142.     SUB r2,r2,#1 ;decrement counter
  143.     ADD  r0,r1,#0x30 ;MSB integer to ascii
  144.     BL  my_putchar  ;outputs an integer
  145.     POP {r1}
  146.     CMP r2,#0
  147.     BGT output
  148.     B   stop
  149.  
  150. error
  151.     LDR r0,=err
  152.     BL  my_fprint
  153.     B stop
  154.  
  155.  
  156. /*END OPERATIONS*/
  157.  
  158. stop B stop
  159.  
  160.     ALIGN       ;aligns code (but also data) to a memory boundary
  161.     AREA MyData, DATA, READWRITE
  162.        
  163. ;strings
  164. err DCB 10,13,"An error occured in processing",0,10,13  ;NULL;LF;CR decimal
  165. cal DCB 10,10,13,"NEW CALCULATOR",0,13  ;NULL;LF;CR decimal
  166. y   DCB "point",0;for debug
  167. ;zero DCB "cannot divide by zero",0
  168. ;inv DCB "invalid input formatting",0
  169.  
  170.     END             ;designates end of source line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement