ARM ASSEMBLY循环遍历argv参数

时间:2015-10-08 07:29:01

标签: loops assembly arm argv

我正在尝试使用argv在命令行中将参数传递给我的程序,我想出了如何指向第一个argv地址,但我似乎无法循环到下一个。

这是我的代码,但我认为相关的是第一个子程序:

 .text
         .global _start
         .equ exit, 1
         .equ write, 4
         .equ stdout, 1

 _start:
    ldr r5, [sp]        @argc value
    ldr r6, =1
    mov r8, #8          @argv address

 0: ldr r4, [sp, r8]        
    add r8, r8, #4   
    mov r1, r4
    adr r10, isbn10 
    adr r11, valid
    adr r12, invalid
    adr r13, isbn13
    bl strlen
    cmp r0, #13
    beq 1f
    cmp r0, #10
    beq 2f

1:  bl check_13
    cmp r2,#0
    bleq print13v
    blne print13i
    add r6, r6, #1 
    cmp r5,r6
    bne 0b
    mov r0, #0      @ success exit code
    mov r7, #exit
    svc 0   

2:  bl check_10
    cmp r2,#0
    bleq print10v
    blne print10i
    add r6, r6, #1 
    cmp r5,r6
    bne 0b
    mov r0, #0      @ success exit code
    mov r7, #exit
    svc 0           @ return to os

val:.asciz "9780306406157"
isbn10:.asciz "\nisbn-10 : "
isbn13:.asciz "\nisbn-13 : "
valid:.asciz ": valid"  
invalid:.asciz ": invalid"
.align 2

strlen:


    mov r0, #0
            @ length to return
0:
    ldrb r2, [r1], #1   @ get current char and advance
    cmp r2, #0      @ are we at the end of the string?
    addne r0, #1
    bne 0b
    mov pc, lr  

#######################


check_13:       @sum at r2
    mov r1, r4
    mov r3,#1       @toggle
    mov r2,#0       @sum
0:  
    ldrb r0,[r1], #1
    cmp r0, #0
    beq 9f
    cmp r0, #'0
    blo 1f
    cmp r0, #'9
    bhi 1f
    sub r0,r0,#'0
    add r2,r2,r0
    cmp r2, #10
    subge r2,r2,#10
    eors r3,r3,#1       @toggled?
    addne r2,r2,r0,lsl#1
    cmp r2, #10
    subge r2,r2,#10
    cmp r2, #10
    subge r2,r2,#10
    bal 0b




1:  mov r2, #22     @returns r2=22 if invalid
    mov pc,lr

9:  mov pc,lr

##################



check_10:       @sum at r2
    mov r1, r4
    mov r3,#0       @t
    mov r2,#0       @sum
0:  
    ldrb r0,[r1], #1
    cmp r0, #0      @end?
    beq 9f
    cmp r0, #'0
    blo 1f
    cmp r0, #'9
    bhi 2f
    sub r0,r0,#'0
    bal 3f

3:  add r3,r3,r0
    cmp r3, #11
    subge r3, r3, #11
    add r2,r2,r3
    cmp r2, #11
    subge r2, r2, #11       
    bal 0b



2:  and r0,r0, #0xdf    @ x becomes x
    cmp r0, #0x58       @ x?
    bne 1f
    mov r0,#10
    bal 3b

1:  mov r2, #22     @returns r2=22 if invalid
    mov pc,lr

9:  mov pc,lr

######################
invalid:
    mov r2, #22     @returns r2=22 if invalid
    mov pc,lr
#######################
print10v:
    mov r9,lr
    mov r1,r10
    bl strlen 
    mov r2,r0
    mov r1,r10  
    mov r0,#stdout
    mov r7, #write @herehere
    svc 0

    mov r1,r4
    bl strlen
    mov r1,r4
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0

    mov r1,r11
    bl strlen
    mov r1,r11
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0
    mov pc,r9

#######################
print10i:
    mov r9,lr
    mov r1,r10
    bl strlen 
    mov r2,r0
    mov r1,r10  
    mov r0,#stdout
    mov r7, #write @herehere
    svc 0

    mov r1,r4
    bl strlen
    mov r1,r4
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0

    mov r1,r12
    bl strlen
    mov r1,r12
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0
    mov pc,r9

#######################
print13v:
    mov r9,lr
    mov r1,r13
    bl strlen 
    mov r2,r0
    mov r1,r13  
    mov r0,#stdout
    mov r7, #write @herehere
    svc 0

    mov r1,r4
    bl strlen
    mov r1,r4
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0

    mov r1,r11
    bl strlen
    mov r1,r11
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0
    mov pc,r9

#######################
print13i:
    mov r9,lr
    mov r1,r13
    bl strlen 
    mov r2,r0
    mov r1,r13  
    mov r0,#stdout
    mov r7, #write @herehere
    svc 0

    mov r1,r4
    bl strlen
    mov r1,r4
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0

    mov r1,r12
    bl strlen
    mov r1,r12
    mov r2,r0
    mov r0,#stdout
    mov r7, #write
    svc 0
    mov pc,r9

我组装并链接后 我使用./validate 9780306406157 1234567890运行它 ISBN-13:9780306406157:有效 ISBN-13:306406157:无效[劣势1(流程22221)正常退出]

意思是r4第二次通过循环得到306406157,我希望它得到1234567890 ......

在做了建议的编辑之后我运行了程序,它在第60行给了我一个分段,当我尝试从新参数中读取一个字节(一个字符)时,我运行gdb并且我注意到了{{{ 1}}(假设第二次通过循环时为r4)与第一次通过循环的值相差很远

argv[2]

任何帮助?

1 个答案:

答案 0 :(得分:1)

ldr r4, [sp, #8] argv[1]时的结果是argv[0][sp, #4] addne r4, r4, #4是执行程序的名称。

因此argv[1]将在argv[2]内提前移动4个字节。加载argv[3][sp, #0xC]等应该采取的措施是阅读[sp, #0x10] mov r8, #8 @ Offset of argv[1] 0: ldr r4, [sp, r8] r4 = argv[n] add r8, r8, #4 n++ mov r1, r4 等。

这样的事情:

<script type="text/javascript">
    $(document).ready(function () {
        $("#deliveryDate").datepicker({
            minDate: 0
        });
    });

    $(document).ready(function () {
        $("#quoationStartDate").datepicker({
            minDate: 0
        });
    });
</script>