Write an ARM assembly language which accepts an integer and
Write an ARM assembly language which accepts an integer and returns a string which is the hexadecimal representation of the integer.
The signature of the routine is:
char * int2hex( int conv ) ;
The input is a 32 bit integer. The output is a pointer to a character string of the format 0Xdddddddd. Do not suppress leading zeroes.
The C program is:
#include <stdlib.h>
#include <stdio.h>
extern char * int2hex( int convert ) ;
int main( int argc, char * argv[] )
{
int convert = 1194684 ;
char * result ;
result = int2hex( convert ) ;
printf( \"Integer to hex string: %s\ \", result ) ;
}
A string in C is terminated by a null byte (a byte of zero).
You will need to use the shift instructions. You need to separate the integer into 4 bit sections each representing the individual hex digits. These need to be turned into printable characters.
You need to ‘adjust’ the resulting values for the digits A-F.
Use the strb instruction and an auto increment of 1 since you are storing bytes into memory.
Use the malloc library routine to obtain space for the character string (11 bytes: 2 for 0x, 8 for the hex digits, and one for the null byte).
Solution
TTL Ch5Ex1
AREA Program, CODE, READONLY
ENTRY
MOV v2, #’0’ ; store ‘0’ at the beginning
LDR v2, [V2, #8]
MOV v2, #’X’ ; store ‘X’ after ‘0’
htoa
STMFD sp!, {v1, v2, lr} ;function entry - save some v-registers
;and the return address.
MOV v1, a1 ; preserve arguments over following
MOV v2, a2 ; function calls
MOV a1, a2
BL div16 ; a1 = a1 / 16
SUB v2, v2, a1, LSL #3 ; number - 8*quotient
SUB v2, v2, a1, LSL #1 ; - 2*quotient = remainder
CMP a1, #0 ; quotient non-zero?
MOVNE a2, a1 ; quotient to a2...
MOV a1, v1 ; buffer ptr unconditionally to a1
BLNE htoa ; conditional recursive call to utoa
ADD v2, v2, #\'0\' ; NUL character
STRB v2, [a1], #1 ; store NUL character at end
LDMFD sp!, {v1, v2, pc} ; function exit - restore and return

