In: Computer Science
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
Source Code:
.data
String: .asciz "1234\n"
Show_integer: .asciz "The integer = %d\n"
Intg: .long 0
.text
.global main
main:
movl $1, %edi
movl $0, %ebx
movl $String, %ecx
character_push_loop:
movb (%ecx), %dl /*Take one byte from the string and put in %dl*/
cmpb $0, %dl
je conversion_loop
movb %dl, (%eax)
pushl %eax /*Push the byte on the stack*/
incl %ecx
incl %ebx
jmp character_push_loop
conversion_loop:
popl %eax
subl $48, %eax /*convert to ASCII number*/
imul %edi /* eax = eax*edi */
addl %eax, Intg
movl $10, %eax
movl %edi, %eax
imul %ecx /* eax = eax*ecx */
movl %eax,%edi /* edi = eax */
decl %ebx
cmpl $0, %ebx
je end /*When done jump to end*/
jmp conversion_loop
end:
pushl Intg
pushl $Show_integer
call printf /*Print out integer*/
addl $8, %esp
call exit
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================