Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in ARM assembly language (which can be executed on ARMSim simula

ID: 3607362 • Letter: W

Question

Write a program in ARM assembly language (which can be executed on ARMSim
simulator) to convert an ASCII string containing positive or negative integer decimal
string to an integer. Input is given as null-terminated string stored at location given by the
label value. The string can containing an optional '+' or '' followed by some
combination of the digits 0 through 9. Your program should compute the integer value
equivalent to this string of digits, then place the number in register R1. If a non-digit
character appears anywhere in the string, your program should stop with the value 1 in
register R1. For example, if input is "24" (or in ASCII 0x32, 0x34, 0x00) then register
R1 should contain the value 24ten. (Hint: The ARM MUL instruction takes two registers
as input. There is no "MULI" instruction. Thus, just store the constant 10 in a register.)

Explanation / Answer

          If the input string is “0”, the output memory at [a1] will contain 1 word (0), and v0 = 1

          If the input string is “452” the output memory at [a1] will contain 1 word (452), and v0 = 1

If the input string is “4294967298”, which is 232+2, then the output memory pointed at by a0 will contain a “2” for the first word (least-significant word) and a “1” for the second word. Further, v0 = 2.

            If the input string is “6B7” the result register, should contain –1

           If the input string is “” (i.e. first character is null), the result register should contain –1

The important portion of the ASCII character table is as follows (Note that values are in HEX notation):

           

ASCII

                      Character                  

Value

                            “0”           0x30       

                            “1”           0x31       

                            “2”           0x32       

                            “3”           0x33       

                            “4”           0x34       

                            “5”           0x35       

                            “6”           0x36       

                            “7”           0x37       

                            “8”           0x38       

                            “9”           0x39       

The MIPS instruction set is included as an attachment. Write this routine in as few instructions as possible on the following page. Assume the virtual MIPS machine (no branch delay slots). Hint: start by writing a “times-ten” routing that takes the current number in your memory register (pointed at by a0 and of some length) and multiplies it by 10. Possibly extending the number of words as necessary. [This page is for your answer]

This is a straightforward extension of the homework exercise. The trickiest part about this solution is the multi-precision arithmetic. The simplest solution is to create an inner loop (timestenplus) that handles the x = x*10+y, where x is the current result in memory and y is a new digit. We accumulate the length of the result in words in v0.

atoapi: st    $r0, 0($a1)

; Default: result = 0

         addi $v0, $r0, 1

     

; one word result so far

ld $t7, 0($a0) beq $t7, $r0, error

nextdigit:    

; Special check for empty string

        ld      $t7, 0($a0)

; Get next digit

         addi $a0, $a0, 1

; Advance string pointer

        bne     $t7, $r0, cont

; Not end of string

        jr      $ra

; Finished – length in $v0

cont: addi $t7, $t7, -0x30

; Convert from ASCII to digit

         slti $t0, $t7, 0

; Less than 0?

        bne     $t0, $r0, error

; Yup. Bad digit

         slti $t0, $t7, 10

; Greater than 9?

        beq $t0, $r0, timestenplus ; No – good digit in $t7

error: addi $v0, $r0, -1

; Error return value

        jr     $ra

timestenplus:  

        add     $t0, $r0, $r0

; Current word count = 0

        add     $t1, $a1, $r0

; Current word pointer = start

         addi $t2, $r0, 10

; Constant for multiplying

loop: lw       $t3, 0($t1)

; Get next word from memory

         mulu $t3, $t2

; Multiply word by 10

         mflo $t3

; Low result of multiply

         addu $t4, $t3, $t7

; Add in new digit     

        sw      $t4, 0($t1)

; Save result in memory

         sltu $t5, $t4, $t3

; Check for carry from addu

         mfhi $t3

; High result of mult

        add     $t7, $t3, $t5

; Add carry from addu

         addi $t0, $t0, 1

; Next word ID

        addi $t1, $t1, 4

     

; Next word pointer

         bne $t0, $v0, loop

; Keep looping until through all digits

     

      beq $t7, $r0, nextdigit ; Finished – no need to increase digits

        st      $t7, 0($t1)

; Add extra word

      addi $v0, $v0, 1       j     nextdigit

; Increase number of words by 1

atoapi: st    $r0, 0($a1)

; Default: result = 0

         addi $v0, $r0, 1

     

; one word result so far

ld $t7, 0($a0) beq $t7, $r0, error

nextdigit:    

; Special check for empty string

        ld      $t7, 0($a0)

; Get next digit

         addi $a0, $a0, 1

; Advance string pointer

        bne     $t7, $r0, cont

; Not end of string

        jr      $ra

; Finished – length in $v0

cont: addi $t7, $t7, -0x30

; Convert from ASCII to digit

         slti $t0, $t7, 0

; Less than 0?

        bne     $t0, $r0, error

; Yup. Bad digit

         slti $t0, $t7, 10

; Greater than 9?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote