Write a program in RISC-V assembly to convert an ASCII string containing a positive or negative integer decimal string t

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write a program in RISC-V assembly to convert an ASCII string containing a positive or negative integer decimal string t

Post by answerhappygod »

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 as input. There isno "muli" instruction. Thus, just store the constant 10 in aregister.For testing purposes: you can add this at the bottom of yourassembly file:.data mystr: .byte 50, 52, 0
DO NOT SEND ME THIS PLEASE :# you can use string as well ...
Write A Program In Risc V Assembly To Convert An Ascii String Containing A Positive Or Negative Integer Decimal String T 1
Write A Program In Risc V Assembly To Convert An Ascii String Containing A Positive Or Negative Integer Decimal String T 1 (324.66 KiB) Viewed 79 times
Program: #str2int.s .data str: .text la x10, str jal x0, str2int jalr str2int: asciiz "-24" 1b x5, (x10) addi x8, x0,45 beq x5, x8, negint addi x8, x0,43 beq x5, x8, posint. jal x0, convert negint: li x19,1 addi x10, x10,1 jal, x0, convert posint: li x19,0 addi x10, x10,1 convert: addi x18,x0,10 add x9,x0, x0 L1: 1b x5,0 (x10) beq x5, x0, exitloop addi x8, x0,48 blt x5, x8, fail addi x8, x0,58 bge x5, x8, fail addi x5,x5,-48 mul x9,x9,x18 add x9,x9,x5 addi x10, x10,1 jal x0, L1 fail: addi x10, x0,-1 jal x0, return exitloop: beq x19, x0, posret neg x9,x9 posret: mv x10,x9 return: jalr x0,0 (x1) #load address of null terminated string into x10 # call function str2int #End of the program #load first byte of the null terminated string #load ascii value of into x8 goto label negint # if first character is #load ascii value of + into x8 #if first character is '+ goto label posint # if first character is neither - nor'+', goto label convert # if first character is -, set flag x19 to 1 #goto address of next byte #goto label convert # if first character is '+', set flag x19 to 0. #goto address of next byte #set x18-10 #set sum (x9) to 0 #load a byte from string # if the current byte is null character, goto exitloop #x8-ascii of '0' # if current character is non-digit character, goto label fail # x8-ascii of ':', (':' is the next character after '9' in ascii chart) #if current character is non-digit character, goto label fail #convert character digit to integer digit #multiply the current result (x9) by 10 # add the current digit to x9 #move to next byte. #repeat the loop L1 #save -1 in x10, if non-digit character appears #goto label return # if x19 is -1, take negation of the sum #copy the integer into x10 #return to caller.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply