使用ARM汇编伪指令编程,计算1-1000中能被6整除的数的个数,并对它们求和
时间: 2024-05-21 12:11:26 浏览: 85
.data
count: .word 0 @ 存储能被6整除的数的个数
sum: .word 0 @ 存储能被6整除的数的和
.text
.global main
main:
mov r1, #1000 @ r1 = 1000
mov r2, #1 @ r2 = 1
loop:
cmp r2, r1 @ 比较r2和r1的值
bgt exit @ 如果r2 > r1,跳转到exit
mov r3, r2, lsr #1 @ r3 = r2 / 2
add r3, r2, r3, lsl #1 @ r3 = r2 + 2 * r3
tst r3, #3 @ 测试r3的末两位是否为0
bne skip @ 如果不是,跳转到skip
add r4, r4, #1 @ 计数器加1
ldr r5, =sum @ 加载sum的地址到r5
ldr r6, [r5] @ 加载sum的值到r6
add r6, r6, r2 @ r6 = r6 + r2
str r6, [r5] @ 存储r6到sum
skip:
add r2, r2, #1 @ r2加1
b loop @ 跳转到loop
exit:
ldr r5, =count @ 加载count的地址到r5
str r4, [r5] @ 存储计数器的值到count
ldr r5, =sum @ 加载sum的地址到r5
ldr r6, [r5] @ 加载sum的值到r6
mov r0, r6 @ 将sum的值赋给r0
bx lr @ 返回
阅读全文