谁能帮助我从32位RISC-V中的控制台阅读64位
我是集会的新手,但是有人可以教我如何从32位RISC-V中的控制台阅读64位吗?
.eqv SYS_EXITO, 10
.eqv CON_PRTSTR, 4
.eqv CON_PRTINT, 1
.eqv CON_RDINT, 5
.eqv BUFSIZE, 100
.data
prompt:
.asciz "Read 64 bit integer:"
result:
.asciz "Output:"
buf:
.space BUFSIZE
.text
main:
la a0, prompt
li a7, CON_PRTSTR
ecall
la a0, buf
li a1, BUFSIZE
li a7, CON_RDINT
ecall
然后我们我输入4294967295,发生了以下错误。
Error in /private/var/folders/bf/t4py6npj0v38grsvrgvq1dx00000gn/T/hsperfdata_sotarosuzuki/riscv1.asm line 24: Runtime exception at 0x00400020: invalid integer input (syscall 5)
因此,我应该将整数读为字符串并将其转换为整数吗? 我已经搜索了此解决方案,但是找不到。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,如果您无法使用玩具系统调用,请阅读字符串并在其上使用
total*10 + digit
,其中digit = c-'0'
。您需要进行扩展推迟乘法,因此进行扩展的precision shifts 喜欢(total&lt;&lt; 3) +(总计&lt;&lt;&lt; 1)< /代码>。
Check compiler output
(RISC-V扩展推迟添加是不方便的,因为它没有随身携带标志,您需要将随身携带效仿为
unsigned sum = a+b;
anctry> crand = sum&lt; a ;
)clang的输出较短,所以我会显示。当然,它遵循标准调用约定,将指针纳入
a0
中,然后在一对寄存器中返回一个64位整数,a1:a0
:如果要:使用GCC的偏移/策略,应该直接查看该插槽如何使用相同的逻辑clang。您可以查看编译器输出的函数,例如
返回U64&lt;&lt; 3
查看哪些说明是其中的一部分。顺便说一句,我牢记了Compent的COMM,我牢记了COMPLING,这使编译器可以轻松地将其转换为
do {},而
循环却在底部具有条件。我的答案中的x86 ASM基于Yeah, if you can't use the toy system calls, read a string and do
total = total*10 + digit
on it, wheredigit = c-'0'
. You'll need to do extended-precision multiply, so it's probably easier to do extended-precision shifts like(total << 3) + (total << 1)
.Check compiler output on Godbolt. For example, GCC using shifts, clang using
mul
/mulhu
(high unsigned) for thelo * lo
32x32=>64-bit partial product, and amul
for the high half cross product (hi * lo
). It's fewer instructions, but depends on a RISC-V CPU with a fast multiplier to be faster than shift/or.(RISC-V extended-precision addition is inconvenient since it doesn't have a carry flag, you need to emulate carry-out as
unsigned sum = a+b;
carry = sum<a;
)Clang's output is shorter, so I'll show it. It of course follows the standard calling convention, taking the pointer in
a0
, and returning a 64-bit integer in a pair of registers,a1:a0
:If you want to go with GCC's shift/or strategy, it should be straightforward to see how that slots in to the same logic clang is using. You can look at compiler output for a function like
return u64 << 3
to see which instructions are part of that.And BTW, I wrote the C with compiling to decent asm in mind, making it easy for the compiler to transform it into a
do{}while
loop with the condition at the bottom. I based it on the x86 asm in my answer on NASM Assembly convert input to integer?