ARM汇编语言:caesar cypher

时间:2014-11-30 00:53:05

标签: arm character

这是为家庭作业

我必须将给定的字符串和偏移量作为参数并创建字符串的加密版本。这是我到目前为止所拥有的

.global
cypher:
    stmfd sp!, {v1-v6, lr} @std
    mov v1, a1             @hold the string pointer in v1
    bl strlen              @get the length of the string in a1
    add a1, a1, #1         @add null byte space to strlen
    mov v2, a1             @hold the length of space needed in v2
    bl malloc              @reserve space for new string in a1
    mov v3, #0             @initial index of new string
loop:ldr v4, [v1], #4      @load v4 with string pointer and increment by bytes
    add v5, v4, a2         @add the offset to the current character
    str v5, [a1, v3]       @store the new character in the new address
    add v3, v3, #4         @increment the index by a byte
    cmp v2, v3
    bne loop
    ldmfd sp!, {v1-v6, pc} @std     
    .end

我无法弄清楚如何正确地增加角色。如何为字符添加偏移量?(我猜测ascii字符需要递增?)

1 个答案:

答案 0 :(得分:0)

您将循环迭代四个字节。

下面是“正确的”:(也是优化的一个:你不需要v3和v5)

loop:ldrb v4, [v1], #1      @load v4 with string pointer and increment by bytes
    subs v2, v2, #1
    add v4, v4, a2         @add the offset to the current character
    strb v4, [a1], #1       @store the new character in the new address
    bne loop

    ldmfd sp!, {v1-v6, pc} @std