写ARM汇编到文件 [英] Write to file in ARM Assembly
问题描述
我试图寄存器的整数内容写入到ARM汇编的文本文件。
我一直在读,但收效甚微 - 我从阅读技术手册,我会需要一些SWI指令推断,但我不能为我的生活找到如何做到这一点的具体实例
I'm attempting to write the integer contents of a register to a text file in ARM Assembly. I have been reading with little success - I have deduced from reading the technical manual I will require some SWI instruction, but I cannot for the life of me find a concrete example of how to do this.
操作系统是皮B型Raspbian运行。
Operating system is Raspbian running on Pi B model.
感谢您预先的任何帮助,如果您需要任何澄清,请让我知道!
Thank you in advance for any help, if you require any clarifications please let me know!
编辑::要澄清,我组装和执行我的code成功地使用GCC。我想我的数据寄存器的值存储到一个文本文件中。
EDIT : To clarify, I am assembling and executing my code successfully using GCC. I want to store the values of my data registers to a text file.
示例:
.global main
.func main
main:
mov r1, #19
mov r2, #11
add r0, r1, r2
bx lr
在上述情况下,R0将持有的价值30.我想该值写入一个文本文件中。
In the above case, r0 would hold the value 30. I want to write that value to a text file.
推荐答案
我这样做给一个尝试,想你需要事先知道的东西。当然也有利用汇编语言编程方法很多,但我的目标毫无来自编译器和库的帮助。
I did give this a try, thinking about things you need to know beforehand. Of course there are many ways to utilize assembly programming but I aimed for no help from the compiler or libraries.
- 能够正确地使用工具链
- 链接器需要一个
_start
符号 - 定义字符串,解决这个问题
- 为
O_WRONLY
,O_CREAT
常量 - 如何连接code许可
- 找到了
打开
,写
,FSYNC $ C $召唤签名C>,
关闭
,退出
- 如何使一个系统调用
- 某种内存管理,我用堆栈
- 指针
- ASCII再一个数字的presentation
- 寄存器加载/存储,寻址模式
-
如何调试!有趣的事情我就开始想,这应该是直截了当才发现,你需要调用硬盘的方式
FSYNC
退出进程要求内核冲厕之前。也许O_DIRECT | O_SYNC
是一个更容易的选择。
- Be able to use toolchain correctly
- Linker expects a
_start
symbol - Define a string, address it
- Constants for
O_WRONLY
,O_CREAT
- How to encode permission
- Find out call signatures of
open
,write
,fsync
,close
,exit
- How to make a syscall
- Some kind of memory management, I used stack
- Pointers
- ASCII representation of a digit
- Load / Store of registers, addressing modes
How to debug! Funny thing I started thinking this should be straight forward only to find out hard way that you need to call
fsync
before exiting the process to ask kernel for flushing. ProbablyO_DIRECT | O_SYNC
is an easier choice.
$ as open_e.s -o open_e.o
$ ld open_e.o -o open_e
$ cat open_e.s
.global _start
_start:
@open syscall
ldr r0, =filename
mov r1, #0101 @ O_WRONLY | O_CREAT
ldr r2, =0666 @ permissions
mov r7, #5 @ 5 is system call number for open
svc #0
cmp r0, #0
blt exit
@r0 contains fd (file descriptor, an integer)
mov r5, r0
@write syscall
@use stack as buffer
sub sp, #8 @ stack is full descending, this is how we leave some space
mov r1, #'4'
strb r1, [sp]
mov r1, #'2'
strb r1, [sp, #1]
mov r1, #'\n'
strb r1, [sp, #2]
mov r1, sp
mov r2, #3
mov r7, #4 @ 4 is write
svc #0
@fsync syscall
mov r0, r5
mov r7, #118
svc #0
@close syscall
mov r7, #6 @ 6 is close
svc #0
exit:
mov r0, #0
mov r7, #1
svc #0
.data
.align 2
filename: .asciz "out.txt"
这篇关于写ARM汇编到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!