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.pdf 和 http://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 字符...)