落絮飞雁

顺流而下,把梦做完

汇编语言数组练习题两则

题目一:在数组中查找某数,查到则返回下标,查不到则返回-1
data段提供如下:

area array, data, readwrite
src    dcd 2,4,3,8,14,1,5
length equ 6*4
num    equ 3
dst    dcd -1,-1,-1,-1,-1,-1,-1

参考解答:

area search, code, readonly
       entry
start  mov r5, #num
       ldr r1, =src
       add r6, r1, #length
       ldr r3, =dst
       mov r4, #0

loop   ldr r2, [r1]
       cmp r2, r5
       BEQ output
inner  add r4, r4, #1
       add r1, r1, #4
       cmp r1, r6
       BLE loop
       B stop

output str r4, [r3]
       add r3, r3, #4
       B inner

Stop    MOV r0,#0x18
    LDR r1,=0x20026
    SWI 0x123456

end


题目二:数组匹配,给两个各自无重复数据的数组,查找里面匹配到的数据,把数据保存到另一个数组里。
data段如下:

area array, data, readwrite
src    dcd 2,4,6,8,10,12,14
len    equ 6*4
dst    dcd 1,2,3,4,5,6,7,8
length equ 7*4
array  dcd 0,0,0,0,0,0,0

参考解答:

area match, code, readonly
       entry
start  ldr r1, =src
       add r5, r1, #len
       ldr r2, =dst
       add r6, r2, #length
       ldr r7, =array

inner  ldr r3, [r1]
       ldr r4, [r2]
       cmp r3, r4
       BEQ output
       add r2, r2, #4
       cmp r2, r6
       BLE inner
outer  add r1, r1, #4
       ldr r2, =dst
       cmp r1, r5
       BLE inner
       B stop

output str r3, [r7]
       add r7, r7, #4
       B outer

Stop    MOV r0,#0x18
    LDR r1,=0x20026
    SWI 0x123456


原文标题:汇编语言数组练习题两则|落絮飞雁的个人网站
原文链接:https://www.luoxufeiyan.com/2015/06/04/asm-array-exercise/
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。