popololvic
popololvic

Reputation: 61

How to declare buffer in ARM Assembly

The goal of my program is to pass it an ASCII string of an integer from the command line to the register r0.

I must then convert the ASCII string to a signed integer and return it in r0, this is done in the procedure atoi, and it works perfectly.

I must then initialize a buffer, and call the procedure itoa, with r0 containing the integer and r1 containing the address of the buffer.

The first problem I run into, as there are many I think, is I cannot seem to initialize and declare a buffer, how do I do that?

this is my code so far: .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

    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

Upvotes: 1

Views: 3677

Answers (1)

Seva Alekseyev
Seva Alekseyev

Reputation: 61388

EDIT: to allocate a buffer in the data segment, use the SPACE directive:

buffer: SPACE 16

Since SPACE is an assembler directive and not a CPU command, this might be different in your assembler.

To load the address into r1, place an address of the buffer into a register-sized constant in the code segment after the end of the function, and then use LDR to load that:

mov pc, lr
p_buffer: dcd buffer

Then load where you need it:

ldr r1, p_buffer

Now r1 contains the contents of p_buffer, which is the address of buffer. Again, the syntax might vary.

Upvotes: 1

Related Questions