Home / Expert Answers / Electrical Engineering / write-a-program-in-risc-v-assembly-to-convert-an-ascii-string-containing-a-positive-or-negative-inte-pa782

(Solved): Write a program in RISC-V assembly to convert an ASCII string containing a positive or negative inte ...



Write a program in RISC-V assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. Your program should expect register x10 to hold the address of a null-terminated string 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 x10. If a non-digit character appears anywhere in the string, your program should stop with the value ?1 in register x10. For example, if register x10 points to a sequence of three bytes 50, 52, 0, which represent the null-terminated string "24\0", then once the program stops, register x10 should contain the integer value of 24 (or 0x18 in hex). Hints: The RISC-V mul instruction takes two registers as input. There is no "muli" instruction. Thus, just store the constant 10 in a register. For testing purposes: you can add this at the bottom of your assembly file: .data mystr: .byte 50, 52, 0 # you can use string as well ...

 

 

this test code does not work for some reason:

1
2 la x10, str #load address of null terminated string into x10
3 jal x0, str2int #call function str2int
jalr # End of the p

 

1 2 la x10, str #load address of null terminated string into x10 3 jal x0, str2int #call function str2int jalr # End of the program 5 str2int: 6 lb x5, 0(x10) # load first byte of the null terminated string 7 addi x8, x0,45 #load ascii value of '-' into x8 8 beq x5, x8, negint #if first character is '-' goto label negint 9 addi x8, x0,43 #load ascii value of '+' into x8 beq x5, x8, posint #if first character is '+' goto label posint 1 jal x0, convert #if first character is neither '-' nor '+', goto label convert 2 negint: 3 li x19,1 #if first character is '-', set flag x19 to 1 4 addi x10,x10,1 # goto address of next byte 5 jal, x0, convert #goto label convert 5 posint: 7 li x19,0 #if first character is '+', set flag x19 to 0 8 addi x10,x10,1 # goto address of next byte convert: addi x18, x0,10 #set x18=10 1 add x9,x0, x0 #set sum(x9) to 0 2 L1: 3 lb x5,0(x10) # load a byte from string 4 beq x5, x0, exitloop #if the current byte is null character, goto exitloop 5 addi x8, x0,48 # x8= ascii of '0' 5 blt x5, x8, fail # if current character is non-digit character, goto label fail 7 addi x8, x0,58 # x8= ascii of ':', (':' is the next character after '9' in ascii chart) 8 bge x5, x8, fail # if current character is non-digit character, goto label fail 9 addi x5, x5,-48 # convert character digit to integer digit


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe