如何使用汇编语言打印从 1 到 n 的数字?



我是汇编语言的新手,我正在尝试使用汇编语言打印 1 到 n 个数字,而不使用 C 库。我正在使用 qemu 作为树莓派的模拟器。

这是我尝试过的示例代码

.global _start
_start:
MOV R7, #3 @ Syscall read from keyboard
MOV R0, #0 @ Input stream keyboard
MOV R2, #1  @ Read 1 characters
LDR R1, =character @ Put number in R1
MOV R6,R1 @Holding value of R1 i.e. the input number 'n'
MOV R1,#0 @initialiing value 
MOV R2, #1 @counter
SWI 0
_loop:
AND R1, R1, R2 @Add one to each value of R1
B _write @print the value
SWI 0

_write:
MOV R7,#4 @ Syscall for output
MOV R0,#1 @ Output stream for R1
MOV R2,#1
CMP R1,R6 @Compare if the value is equal to 'n'
BNE _loop @if less than, then add again and print
SWI 0
end:
MOV R7,#1
SWI 0
.data
character:
.word 0

请让我知道,应该怎么做。

谢谢

我注意到几件事不对劲。当一个例程被调用(SWI(时,寄存器可以改变,所以你应该把这些值保存在你正在使用的寄存器中(push {r1, r2, r6, lr}(,并在例程(pop {r1, r2, r6, lr}(之后恢复它们,期望r1和r2。AND指令不是一个好的加法器,请使用add。在_loop:有一个额外的SWI 0说明。在_write:中,寄存器r1中需要有character的地址,并且要写入的数据需要在内存中标记为character可打印字符(0x00000001不是可打印字符,添加0x30或十进制48将使其成为可打印字符(。

我对你的程序的修改是小写的。我将main添加到顶部,以防您想使用gdb.我跳过/绕过了输入并对输入进行了硬编码。

.global _start
main:
_start:
@MOV R7, #3 @ Syscall read from keyboard
@MOV R0, #0 @ Input stream keyboard
@MOV R2, #1  @ Read 1 characters
@LDR R1, =character @ Put number in R1
@MOV R6,R1 @Holding value of R1 i.e. the input number 'n'
@MOV R1,#0 @initialiing value
@MOV R2, #1 @counter
@SWI 0
mov  r6, #5
mov  r1, #0
mov  r2, #1
_loop:
@AND R1, R1, R2 @Add one to each value of R1
add  r1, r1, r2
B _write @print the value
@SWI 0

_write:
push {r1, r2, r6, lr}
add  r3, r1, #48
ldr  r0, =character
str  r3, [r0]
MOV R7,#4 @ Syscall for output
MOV R0,#1 @ Output stream for R1
ldr r1, =character
MOV R2,#1
SWI 0
pop {r1, r2, r6, lr}
CMP R1,R6 @Compare if the value is equal to 'n'
BNE _loop @if less than, then add again and print
end:
MOV R7,#1
SWI 0
.data
character:
.word 0

组装、链接和输出

as -o count2.o count2.s
ld -o count2 count2.o
./count2
12345

加法:

我没有使用过 QEMU,但查看 https://azeria-labs.com/load-and-store-multiple-part-5/我认为问题可能是访问内存存储。试试这个,看看它是否有区别。我还更改了pushpop指令。

.global _start
_start:
@MOV R7, #3 @ Syscall read from keyboard
@MOV R0, #0 @ Input stream keyboard
@MOV R2, #1  @ Read 1 characters
@LDR R1, =character @ Put number in R1
@MOV R6,R1 @Holding value of R1 i.e. the input number 'n'
@MOV R1,#0 @initialiing value
@MOV R2, #1 @counter
@SWI 0
mov  r6, #5
mov  r1, #0
mov  r2, #1
_loop:
@AND R1, R1, R2 @Add one to each value of R1
add  r1, r1, r2
B _write @print the value
@SWI 0

_write:
@push {r1, r2, r6, lr}
stmdb sp!, {r1, r2, r6, lr}
add  r3, r1, #48
@ldr  r0, =character
ldr  r0, character_address
str  r3, [r0]
MOV R7,#4 @ Syscall for output
MOV R0,#1 @ Output stream for R1
@ldr r1, =character
ldr r1, character_address
MOV R2,#1
SWI 0
@pop {r1, r2, r6, lr}
ldmia sp!, {r1, r2, r6, lr}
CMP R1,R6 @Compare if the value is equal to 'n'
BNE _loop @if less than, then add again and print
end:
MOV R7,#1
SWI 0
character_address:
.word character
.data
character:
.word 0

最新更新