I’ve a very precise specification for an assembly subroutine:
Specification
Name: The subroutine must be called hexasc. Input parameters: Only
one, in register r4. The 4 least significant bits in register r4
specify a number, from 0 through 15. The values of all other bits in
the input must be ignored. Return value: Only one, returned in
register r2. The 7 least significant bits in register r2 must be an
ASCII code as described below. All other bits in the output must be
zero. Required action: Input values 0 through 9 must be converted to
the ASCII codes for the digits ‘0’ through ‘9’, respectively. Input
values 10 through 15 must be converted to the ASCII codes for the
letters ‘A’ through ‘F’, respectively. Side effects: The values in
registers r2 through r15 may be changed. All other registers must have
unchanged values when the subroutine returns.
I might know how to make a subroutine but I never did that before. I don’t know how to specify the 4 least significant bits from a register. I don’t know how to return values. I’m just getting started with assembly programming and I can run but not really write programs. Could you guide me with some helpful hints? The processor manual is available here.
The best I can propose, all which Ido not understand since I borrowed some of this code from the internet, is
.global main
.text
.align 2
main: movi r8, 0x09
movi r9, 0x0f
andi r4, r4, 0x0f
bgt r8, r4, L1
movi r2, 0x1e
add r2, r2, r4
andi r2, r2, 0xff
movia r2,putchar
br L2
L1: movi r2, 0x29
add r2, r2, r4
andi r2, r2, 0xff
movia r2,putchar
L2: .end
I’ve commented the code but I think it is not according to spec since it is not a subroutine and I’m not sure whether the conversion algorithm is implemented correctly:
.global hexasc
.text
.align 2
hexasc: movi r8, 0x09 #we are going to use the decimal number 9
movi r9, 0x0f #we also will use decimal number 15
andi r4, r4, 0x0f #keep only last 4 bits of what is in r4
bgt r8, r4, L1 #go to L1 if 9 > x
movi r2, 0x1e #use decimal number 30
add r2, r2, r4 #add 30 and what is in r4
andi r2, r2, 0xff
movia r2,putchar
br L2
L1: movi r2, 0x29 #this is reached iff 9 > x
add r2, r2, r4
andi r2, r2, 0xff
movia r2,putchar
L2: .end
Since this is homework it’s your own task to write code, but maybe this can help you:
If you have a register of arbitrary size, and want only specific bits from it, consider AND-ing with a mask. For example, for the 7 least significant bits
Regarding I don’t know how to return values, the answer is written in your assignment:
Simply store whatever you want to return in register r2 before your routine ends.