ARM Assembly - a few simple exercises

Discussion in 'Embedded Systems and Microcontrollers' started by sandy.bridge, Nov 12, 2013.

  1. sandy.bridge

    Thread Starter New Member

    Nov 12, 2013
    I have a couple exercises that are supposed to be simple. This will be my first time coding in assembly. I have always used C, so bear with me. Just looking for assurance that I am doing it correctly.

    Question 1. Write a subroutine to convert a null-terminated string of lower case letters into upper case letters. The original data is in RAM, so this routine overwrites the string. A pointer to the string is passed by reference in register 0 (R0).

    To convert from lower case to upper case, one must subtract by 32. [A, Z] = [65, 90] and [a, z] = [97, 122].

    Here is my subroutine:
    Code ( (Unknown Language)):
    2. CMP R0, #122
    3. BHI skip ; if(R0 > 122) then jump to next skip
    4. CMP RO, #96
    5. SUBHI R0, R0, #32 ; if(R0>96){R0-=32}
    6. skip
    Next example I did involved a comparison of two null-terminated strings. A pointer to the first string is passed by R0 and a pointer to the other is passed by R1. If they are equal R0 is passed as 1 and 0 if they are not.
    Code ( (Unknown Language)):
    2. CMP R0, R1
    3. BEQ skip ; if(R0==R1), jump to next skip
    4. AND R0, R0, #0x00000000 ; R0=R0&0
    5. BNE skip2
    6. skip AND R0, R0, #0x00000000
    7. ORR R0, R0, #0x00000001
    8. skip2
    The last example I did involved writing a subroutine Average in assembly that calculated the average of three unsigned 32-bit numbers. The three paramters are passed by way of the stack. Our result is to return in R0. A typical calling sequence is as follows
    Code ( (Unknown Language)):
    2. MOV R0, #200
    3. MOV R1, #300
    4. MOV R2, #400
    5. PUSH (R0-R2}
    6. BL Average
    What I did was as follows:
    Code ( (Unknown Language)):
    1. Average POP {R0-R2} ; load values on stack into R0, R1, R2
    2. ADD R0, R0, R1
    3. ADD R0, R0, R2
    4. UDIV R0, R0, #3
    5. BX R0
    Thanks in advance!
    Last edited: Nov 12, 2013