Advertisement
chobobdev

Untitled

Jun 12th, 2022
1,313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 4.60 KB | None | 0 0
  1.                 ;       Assume that there are two IEEE-754 single precision floating point numbers stored in my_data.
  2.                 ;       Calculate the sum of these two numbers and store it in R8
  3.                 ;       Your program's results should be same as/similar to what happens in real C/Java programs.
  4.                
  5.                 ;       Suggestion: write functions that can extract the sign bit, mantissa and exponent
  6.                
  7.                 ;       numbers will not lead to overflow. Your program does not need to consider overflow..
  8. my_data         DCD     0xC0350000, 0x40310000
  9.                
  10.                 adr     r12,my_data
  11.                 mov     r11,r12
  12.                 ldr     r1,[r11]
  13.                 add     r11,r11,#4
  14.                 ldr     r2,[r11]
  15.                
  16. addfloat
  17.                 ldr     r9, =0x7f800000
  18.                 and     r3, r1, r9               ; get the first number's exponent from the string
  19.                 and     r4, r2, r9               ; get the second number's exponent from the string
  20.                 cmp     r3, r4
  21.                
  22.                 movcc   r10, r1
  23.                 movcc   r1, r2
  24.                 movcc   r2, r10                  ; if r2 has the higher exponent swap r1 and r2 for easier calculation
  25.                 andcc   r4, r1, r9
  26.                 andcc   r5, r2, r9               ; update exponents if swapped to each registers
  27.                
  28.                 mov     r3, r3, lsr #23         ; exponents to least significant position
  29.                 mov     r4, r4, lsr #23          ; exponents to least significant position
  30.                
  31.                 sub     r10, r3, r4              ; calculate the shifted exponents
  32.                 ldr     r9, =0x007fffff
  33.                 and     r5, r1, r9               ; grab mantissa of first number
  34.                 and     r6, r2, r9               ; grab mantissa of second number
  35.                 ldr     r9, =0x00800000
  36.                 orr     r5, r5, r9               ; add 1 to first fractional part
  37.                 orr     r6, r6, r9               ; add 1 to second fractional part
  38.                 mov     r6, r6, lsr r10          ; shift r6 to the right as much as the exponent difference
  39.                
  40.                 ldr     r9, =0x80000000
  41.                 ands        r0, r1, r9               ; check msb to check if its a negative number
  42.                 movne   r0, r5                   ; if negative copy the value in r5 to r0
  43.                 stmfdne sp!, {lr}               ; save the current position to come back after function
  44.                 blne        twos_complement          ; apply two's complement
  45.                 ldmfdne sp!, {lr}               ; load the position
  46.                 movne   r5, r0              ; move the inveted value back to r5
  47.                
  48.                 ands        r0, r2, r9               ; check msb to check if a negative number
  49.                 movne   r0, r6              ; if negative copy the value into r0
  50.                 stmfdne sp!, {lr}               ; save the current position to come back after function
  51.                 blne        twos_complement          ; two's complement fractional second number if it's supposed to be negative
  52.                 ldmfdne sp!, {lr}               ; load the position
  53.                 movne   r6, r0              ; move the inverted value back into r6
  54.                
  55.                 add     r5, r5, r6               ; add two mantissa, save it into r5.
  56.                
  57.                 ands        r0, r5, r9               ; check msb to see if the result is negative
  58.                 movne   r0, r5              ; if negative, copy the value of r5 into r0
  59.                 stmfdne sp!, {lr}               ; save the current position to come back after function
  60.                 blne        twos_complement          ; two's complement result if negative
  61.                 ldmfdne sp!, {lr}               ; load the position
  62.                 movne   r5, r0              ; copy the ro to r5
  63.                
  64.                 ldrne   r0, =0x80000000          ; put a 1 as msb for result if negative
  65.                 moveq   r0, #0                   ; put a 0 as msb for result if positive
  66.                
  67.                 mov     r10, #0
  68.                 ldr     r9, =0x80000000
  69.                 bl      final_step              ; merge the results together
  70.                 mov     r8,r0               ;copy the value of r0 to r8 as instructed
  71.                 b       exit
  72.                
  73.                
  74. final_step
  75.                 cmp     r9, r5
  76.                 addhi   r10, r10, #1
  77.                 movhi   r9, r9, lsr #1
  78.                 bhi     final_step              ; count how many times you have to shift before hitting a 1 in the result
  79.                
  80.                 cmp     r10, #8                  ; if it's shifted 8 times it's already in the right place
  81.                 subhi   r10, r10, #8                ;  count the numbers of shift needed on left direction
  82.                 movhi   r5, r5, lsl r10         ; shift as needed
  83.                 subhi   r4, r4, r10             ; subtract shift amount from exponent to unshift
  84.                 movcc   r9, #8
  85.                 subcc   r10, r9, r10            ; count the numbers of shift needed on right direction
  86.                 movcc   r5, r5, lsr r10         ; shift if needed
  87.                 addcc   r4, r4, r10             ; add shift amount to exponent to unshift the value
  88.                
  89.                 mov     r4, r4, lsl #23             ; shift exponent into place
  90.                 orr     r0, r0, r4                  ; if shifted, exponent into number
  91.                 ldr     r9, =0x007fffff
  92.                 and     r5, r5, r9              ; get rid of implied 1 in mantissa
  93.                 orr     r0, r0, r5              ; attach mantissa part
  94.                
  95.                 mov     pc, lr
  96.                
  97. twos_complement                             ; invert the numbers
  98.                 mvn     r0, r0                      ; negate r0
  99.                 add     r0, r0, #1                  ; add 1
  100.                 mOV     pc, lr                      ; Return to caller
  101. exit
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement