【问题标题】:How to get an integer from standard input in ARMSIM# (Without using scanf?)如何从 ARMSIM# 的标准输入中获取整数(不使用 scanf?)
【发布时间】:2016-03-02 19:39:19
【问题描述】:

所以我在谷歌上搜索了两天,但没有找到关于这个问题的运气。我正在使用 ARMSIM#,手册中没有任何地方解释如何从标准输入中获取整数,更不用说如何使用它了!有没有更简单的方法可以在不使用 scanf 的情况下做到这一点?如果没有,我该如何使用scanf?我不知道能够使用 scanf 的语法(或库调用?),它给了我一个“未知符号错误”。

*MODS,请不要链接这个thread作为答案。它不起作用。

【问题讨论】:

  • 刚刚尝试过 ARMsim 1.9.1,如果 stdin 设置为来自文本文件(在首选项中),文件描述符 #0 上的 SWI_ReadInt 会按预期工作。看起来它只是马车......
  • 您能详细解释一下吗?这听起来很有趣。我只是将“.txt”放在首选项中吗?那么文件描述符 0 等的语法会是什么?
  • 呃,不是这样。我查看了首选项窗口,似乎很清楚将文件路径放在带标签的文本框中会重定向 I/O。我试过了,确实做到了。浏览和输出模式按钮也不起作用,这让我更加相信整个标准 I/O 实现有点垃圾。

标签: assembly arm


【解决方案1】:

Assembly 与 C (scanf,...) 完全不同。组装直接在芯片上工作,并直接使用其寄存器和外围设备(寄存器、ALU、GPIO、TWI 等接口......)

例如看这个帖子Assembly: Read integer from stdin, increment it and print to stdout

这是针对 IA32 汇编器的,ARM 汇编器又是不同的,即从标准输入读取的系统调用有另一个数字,...

在这个线程中是 ARM 汇编器:Reading and Printing a string in arm assembly

要从 ARMSIM 中的文件读取整数,请使用系统调用 SWI 0x6a(STDIN 在内部被视为文件,在大多数情况下(总是)具有 FD0)

STDIN 的文件句柄(文件描述符)是 0

在这个 git 中有一些 armsim 代码,但它未完成(错误)https://github.com/lseelenbinder/armsim/blob/master/test_files/sim2/sim2os/armos.c

在以下代码中,pdf http://cas.ee.ic.ac.uk/people/gac1/Architecture/Lecture10_5.pdf 用作 ARM SWI(系统调用)的参考

根据此 pdf,SWI 0x6a 从文件句柄中读取给定数量的字节。现在输入的编码通常很重要,它是 KeyCode,请参见 http://cas.ee.ic.ac.uk/people/gac1/Architecture/Lecture10_5.pdfhttp://www.theasciicode.com.ar/(对于大写字母和数字,KeyCode 和 ASCII 相同) 因此,当一个键在键盘上敲击时,STDIN 中出现 1 个字节 所以数字 1,2,3,4,5,6,7,8,9,0 都由 1 个字节组成(参见 KeyCode/ASCII 表)。如果要读取 4 位数字,则必须读取 4 个字节

 AREA read_from_stdin, CODE

.equ SWI_Open, 0x66    ;open a file
.equ SWI_Close,0x68    ;close a file
.equ SWI_PrChr,0x00    ; Write 1 byte to file handle
.equ SWI_RdBytes, 0x6a ; Read n bytes from file handle
.equ SWI_WrBytes, 0x69 ; Write n bytes to file handle
.equ Stdin, 0          ; 0 is the file descriptor for STDIN
.equ Stdout, 1         ; Set output target to be Stdout
.equ SWI_Exit, 0x11    ; Stop execution

ENTRY

START mov R0,#0        ; the file handle from that is read has to be in R0 the file handle for STDIN is 0
adr R1, =buffer        ; load address of the buffer in which is read to R1
mov R2,#4              ; read 4 bytes for a 4 digit number (4 characters)
swi 0x6a               ; invoke system call 0x6a
                       ; now type a number, the corresponding KeyCode  should appear in buffer
; to print the content of buffer do

mov R0,#1              ; write to stdout
adr R1, =buffer        ; move address of buffer in R1 to write the content of buffer
mov R2,#4              ; write 4 bytes (4 characters)
swi 0x69               ; invoke system call 0x69
                       ; this should write the content of buffer you typed to stdout

buffer % 4             ; reserve buffer 4 byte

END

可能包含语法错误,我只知道 ARM 程序集不知道 ARMSIM#

这样做的问题是从标准输入读取 4 个字节 (char) 不会使这 4 个字节成为 int (https://en.wikipedia.org/wiki/Integer_(computer_science))。当在键盘上敲击键时,STDIN 中会出现 KeyCode-/ASCII 编码的字符,您必须对它们进行整数化...(我首先会跳过所有不是数字的 ASCII 字符...)

【讨论】:

  • 这看起来很棒。我认为前两个链接虽然是重复的 :O 但除此之外非常感谢您抽出宝贵的时间来写这篇文章。所以你的意思是我应该能够使用 SWI 0x6c 来获取我的 STDIN 中的任何内容?当你说找到 STDIN 的文件句柄时,你到底是什么意思?
猜你喜欢
  • 1970-01-01
  • 2018-04-28
  • 2018-08-31
  • 2022-09-24
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多