Saturday, 22 June 2013

Improving the output format

  • This subroutine to display a vector of words using different strings for first and subsequent elements
  • # this subroutine prints vectors of words
    #
    # inputs
    #   r0 - start of vector
    #   r1 - number of elements to print
    #   r2 - pointer to start of string used to print first element
    #   r3 - pointer to start of string used to print subsequent elements
    #
    # no outputs
    #
    .globl _vprintw
    .equ datum_size,4                @ working with 4 byte (32 bit) words
    _vprintw:
     stmfd sp!, {r4, r5, r6, r7, lr} @ save registers on the stack
     cmp r1, #0                      @ exit if no elements
     ble last
     mov r4, r0                      @ copy the parameters to locals
     mov r5, r1
     mov r6, r2
     mov r7, r3
     ldr r1, [r4], #datum_size       @ load first vector element to r0 and bump pointer
     mov r0, r6                      @ address of first string to r0
     bl  printf                      @ and print it
     nop
     subs r5, r5, #1                 @ decrement counter
     beq last                        @ and fall out if zero
    vprintw_loop:
     ldr r1, [r4], #datum_size       @ load next vector item to r0 and bump pointer
     mov r0, r7                      @ address of subsequent string to r0
     bl  printf                      @ and print it
     subs r5, r5, #1                 @ decrement counter
     bne vprintw_loop                @ and loop if non-zero
    last:
    
     ldmfd sp!, {r4, r5, r6, r7, pc} @ restore registers from stack and return
    
    

    A simple test harness to use the subroutine is given by

    .globl main
    .section .rodata
    first:
     .asciz "Vector of words - values : %d"
    subsequent:
     .asciz ", %d"
    final:
     .asciz "\n"
    values:
     .word 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
    endvalues:
    .align 2
    .text
    main:
            ldr r0, =values
            mov r1, #11
            ldr r2, =first
            ldr r3, =subsequent
            bl _vprintw              @ print the vector elements
            ldr r0, =final
            bl printf                @ print the terminating newline
    
            mov     r7, #1           @ set r7 to 1 - the syscall for exit
            swi     0                @ then invoke the syscall from linux
    
    bob@poland:~/www/examples$ make test_vprintw
    /usr/bin/gcc -gstabs -o test_vprintw test_vprintw.s vprintw.s
    bob@poland:~/www/examples$ ./test_vprintw
    Vector of words - values : 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
    bob@poland:~/www/examples$
    

    No comments:

    Post a Comment