在 ARM 程序集中写入文件 [英] Write to file in ARM Assembly

查看:15
本文介绍了在 ARM 程序集中写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将寄存器的整数内容写入 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.

操作系统是运行在 Pi 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!

澄清一下,我正在使用 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_WRONLYO_CREAT 的常量
  • 如何编码权限
  • 找出openwritefsynccloseexit的调用签名代码>
  • 如何进行系统调用
  • 某种内存管理,我使用了堆栈
  • 指针
  • 数字的 ASCII 表示
  • 加载/存储寄存器、寻址模式
  • 如何调试!有趣的是,我开始认为这应该是直截了当的,只是为了找出在退出进程之前需要调用 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. Probably O_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆