Write a program in RISC-V assembly to convert an ASCII stringcontaining a positive or negative integer decimal string to aninteger. Your program should expect register x10 to hold theaddress of a null-terminated string containing an optional "+" or"−" followed by some combination of the digits 0 through 9. Yourprogram should compute the integer value equivalent to this stringof digits, then place the number in register x10. If a non-digitcharacter appears anywhere in the string, your program should stopwith the value −1 in register x10. For example, if register x10points to a sequence of three bytes 50, 52, 0, which represent thenull-terminated string "24\0", then once the program stops,register x10 should contain the integer value of 24 (or 0x18 inhex). Hints: The RISC-V mul instruction takes two registers asinput. There is no "muli" instruction. Thus, just store theconstant 10 in a register. For testing purposes: you can add thisat 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 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
Write a program in RISC-V assembly to convert an ASCII string containing a positive or negative integer decimal string t
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am