如何在ARM Assembly中声明缓冲区

时间:2015-10-20 01:06:18

标签: assembly arm buffer

我的程序的目标是从命令行向寄存器r0传递一个整数的ascii字符串。 然后我必须将ascii字符串转换为有符号整数并将其返回到r0,这在atoi过程中完成,并且它完美地工作。 然后我必须初始化一个缓冲区,并调用过程itoa,其中r0包含整数,r1包含缓冲区的地址。 我遇到的第一个问题,就像我认为的那样,我似乎无法初始化并声明缓冲区,我该怎么做?

到目前为止这是我的代码:                         。文本         .global _start         .equ出口,1         .equ写,4         .equ stdout,1

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

    ldr r4, [sp, r8]        
    add r8, r8, #4          
    mov r0, r4
    bl atoi
    ldr r1, =buffer 
    bl itoa
    ldr r0, =1
    bl println
    mov r0, #0      @ success exit code
    mov r7, #exit
    svc 0           @ return to os

atoi:
    push {r4, lr}
0:
    ldrb r1, [r0], #1
    cmp r1, #'-
    beq 2f
    cmp r1, #'0
    blo 1f
    cmp r1, #'9
    bhi 1f
    sub r1,r1,#'0
    ldr r3, =0
    mov r3,r2,lsl#3
    add r2,r2,r2
    add r2,r2,r3
    add r2,r2,r1
    bal 0b

1:  cmp r4, #1
    rsbeq r2, r2, #0
    mov r0, r2
    pop {r4, pc}

2:  
    ldr r4, =1
    bal 0b

itoa: 
    push {r4, r5, lr}
    mov r4, r1


    ldr r5, =0
    push {r5}
    cmp r0,#0
    bgt 4f

    0:  ldr r5, =45
    strb r5, [r4] , #1
    rsb r0, r0, #0

4:  b qr10

3:  add r1, r1, #'0
    push {r1}
    cmp r0, #0
    bne 4b

1:  pop {r5}
    cmp r5, #0
    beq 2f
    strb r5, [r4] , #1
    bal 1b

2:  ldr r1, =buffer 
    pop {r4,r5, pc}



qr10:
    mov r3, r0      @ save dividend (n)
    ldr r1, =0x1999999a @ 2^32/10
    sub r0, r0, r0, lsr #30 @ adjust for large dividends
    umull r2, r0, r1, r0    @ quotient in r0 (q)
    mov r1, r0, LSL #3  @ 8q
    add r1, r1, r0, LSL #1  @ 10q
    sub r1, r3, r1      @ remainder in r1 (r = n - 10q)
    b 3b



# determine string length
# parameters
#   r0:   address of null-terminated string
# returns
#   r0:   length of string (excluding the null byte)
# modifies r0, r1, r2
strlen:
    @ push {lr}
    mov r1, r0      @ address of string
    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
# return
    @ pop  {pc}
    mov pc, lr      @ can do this instead of using the stack

# write a null-terminated string followed by a newline
# parameters
#   r0:  output file descriptor
#   r1:  address of string to print
# modifies r0, r1, r2
println:
    push {r4, r5, r7, lr}
# first get the string length
    mov r4, r0      @ save the fd
    mov r5, r1      @ and the string address
    mov r0, r1      @ the string address
    bl strlen       @ returns the string length in r0
    mov r2, r0      @ put length in r2 for the WRITE syscall
    mov r0, r4      @ restore the fd
    mov r1, r5      @ and the string address
    mov r7, #write
    svc 0
    mov r0, r4      @ retrieve the fd
    adr r1, CR      @ get the address of the CR string
    mov r2, #1      @ one char to write
    mov r7, #write
    svc 0
    pop {r4, r5, r7, pc}    @ restore registers and return to caller

    CR: .byte '\n
    .data
    buffer:  .space 32

1 个答案:

答案 0 :(得分:1)

编辑:要在数据段中分配缓冲区,请使用SPACE指令:

buffer: SPACE 16

由于SPACE是汇编程序指令而不是CPU命令,因此汇编程序可能会有所不同。

要将地址加载到r1中,请在函数结束后将缓冲区的地址放入代码段中的寄存器大小常量,然后使用LDR加载:

mov pc, lr
p_buffer: dcd buffer

然后在您需要的地方加载:

ldr r1, p_buffer

现在r1包含p_buffer的内容,它是缓冲区的地址。同样,语法可能会有所不同。