Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic ARM Assembly help

Options
  • 15-11-2015 8:40pm
    #1
    Registered Users Posts: 476 ✭✭


    I am looking for help in turning a decimal integer into hexadecimal. I received user input in ASCII form and use the code:-
    LDR R5, = 10
    BL getkey
    SUB R0, R0, #0x30
    MUL R4, R5, R4
    ADD R4, R0, R4

    After doing calculations using the inputted data I need to convert it back. One way I was thinking is to divide by the constantly and add the modulus together, however, this would print out the integer the wrong way around e.g 1234 would print out 4321 as the 4 would be taken out first and then multiplied by 10. I was also thinking of a way I could divide by the number of digits present and then divide by 10 each time e.g 1234/1000 = 1 then 234/100 = 2 , however, for this to work I must know the length of the integer which I will not as the user could enter 2x2 as easily as 999x999x999. Any ideas would be greatly appreciated.


Comments

  • Registered Users Posts: 476 ✭✭RoRo979


    here is my go at it, I feel it could be cleaner however.
    output
    LDR R2, = 0 ;
    LDR R6, = 0 ; this will increment

    CMP r5, #0 ; Find out if number is negative
    BGE endifneg ; if positive branch to endifneg
    RSB r5, r5, #0 ; else find absolute value and print out a ‘-‘
    LDR R0, = #0x2D
    BL sendchar
    endifneg

    Reset
    LDR R7, = 8 ; highest power of 10
    SUB R7,R6, R7 ; Each branch take 1 more away to find the highest
    MOVS R8, R7 ; Make a temporary version of the counter
    LDR R9,=1; ; Use this to multiply the 10 by
    whmul CMP R8, #1 ; while (tmp > 1)
    BLS endwhmul ; {
    MUL r9, r10, r9 ; result = result × tmp
    SUB r8, r8, #1 ; tmp = tmp - 1
    B whmul ; }
    endwhmul

    checker
    Add R6, R6, #1 ; When max power found add 1 to the number to take away
    CMP R5, R9 ; compare the highest factor to the number
    BLO endiflabel ; if it is too big branch and multiply by a power less
    B Reset
    endiflabel

    Repeat
    CMP R5, #0 ; if the number is 0 terminate the program
    BLO endRepeat ;
    CMP R5, R9 ; if R9 IS greater than R5 print out the character contained
    BLO endRepeat ;
    SUB R5, R5, R9 ; minus the highest divisor of power of 10 from number
    ADD R2, R2, #1 ; add to the number to be converted into ASCII
    MOV R0, R2 ; Move register R to R0
    ADD R0, #0x30 ; Convert into ASCII
    B Repeat
    endRepeat
    BL sendchar ; Print the number
    CMP R5, #0 ; If numbers are still left then start again with n-1 power
    BGT Reset
    endoutput


  • Closed Accounts Posts: 2,267 ✭✭✭h57xiucj2z946q


    You'd probably get a more meaningful answer on stack-overflow to be honest.

    However, for reference, I would recommend writing a solution in C. Compile and disassemble the binary to see the resulting assembly code.

    You can experiment with different compiler optimizations do see how tight/compact it can be made. Start off with no optimizations first however, so you can easier follow the resulting ASM.


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    "turning a decimal integer into hexadecimal. I received user input in ASCII form "

    ASCII 0 is 48, 1 - 49 and so on. Let's say that you got a number 2345, so in ASCII string it's 50-51-52-53.
    Take the string, substract 48 from each ASCII character and you got the number. Then use 4 bit mask (like b00001111) and shift operation to split the number into 4 bit nibbles. Convert nibbles into hex (0-9 --> add 48, 10-15 --> add 65) and you have a string with ASCII hex number.

    I never wrote a line of ARM assembler, but I guess some things are the same as for ZX81 or x86.


Advertisement