admin管理员组文章数量:1332974
I'm new in the assembly universe and I can't find a simple way to write a number value in console with RISC-V64. I want to do something like that :
.data
x: .word 10
y: .word 2
.text
.global _start
_start:
li a0, 1
lw s0, x
lw s1, y
add a1, s0, s1
li a2, 2
li a7, 64
ecall
But I have a segmentation fault. Thanks
If you want any information about my problem, ask me !
The same program can write string without problem...
I'm new in the assembly universe and I can't find a simple way to write a number value in console with RISC-V64. I want to do something like that :
.data
x: .word 10
y: .word 2
.text
.global _start
_start:
li a0, 1
lw s0, x
lw s1, y
add a1, s0, s1
li a2, 2
li a7, 64
ecall
But I have a segmentation fault. Thanks
If you want any information about my problem, ask me !
The same program can write string without problem...
Share Improve this question asked Nov 20, 2024 at 16:17 TartineVaillanteTartineVaillante 31 bronze badge 1- What you are doing here is initializing two variables in the .data segement, which both have the value 10 and 2. Then you try to load the data from those adresses and probably the OS doesn't let you access those addresses. So either run bare metal or initialize a proper memory address – Lovis XII Commented Nov 20, 2024 at 22:50
1 Answer
Reset to default 0You're using the Write
system call, which writes bytes to a file, possibly stdout
, but could be any open file.
Its signature is Write ( int fileHandle, char *byteBuffer, int byteCount );
.
You have not provided the proper byte pointer for byteBuffer
parameter, instead you have provided a simple integer that is not a pointer, so when it interprets the buffer
parameter as a pointer as it is expecting, it crashes. This is a classic type mismatch, that a high level language would have prevented, but type mismatches are among many errors that assembly language is willing to let you make without build-time complaint.
In order to print bytes using Write
, you will need to provide a memory address (pointer) to the bytes you want to write. However, to start from a number to print, you will also need to convert the integer (result of the addition) from numeric form into a byte string in memory, then pass the address of that byte string and its length to Write
.
Most computer systems have a way to write ascii/unicode bytes, so to print a number using these facilities, convert the number to a byte string of ascii characters that represents the number in some number base like decimal, then print that byte string.
If using RARS, and you want to simply print an integer to the console in decimal, there is a different ecall
, named PrintInt
, and it will print the integer you place into a0
. It is ecall
number is 1 (goes into a7
). This is kind of a simple and easy way to print integers to the console (and even though it internally converts the number to a byte string for console output, there's no way to use this one to make a byte string for other purposes).
There's no options with ecall
1 (printInt) to print in different number base or to print leading zeros, or to print unsigned.
Were you to want any of that you'd be looking back at Write
and supplying the byte string of characters to print with some additional programming often referred to as itoa
(i.e. integer to ascii).
本文标签: riscvHow to print an integer with RISCV assemblyStack Overflow
版权声明:本文标题:riscv - How to print an integer with RISC-V assembly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742345522a2457443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论