The assigment is to learn assembly programming by writing a subroutine that can convert between 4-bit hexadecimal and 7-bit ASCII. At first I had no idea but after some research I could make and effort and draw a flowchart and make a program but it is not entirely correct so I'm asking for your guidance to help me solve this.
The actual assignment text is this:
HA 3.1. Draw a flow-chart for a subroutine to convert a 4-bit hexadecimal value to the corresponding 7-bit ASCII-code. See the full specification for hexasc below. Example: binary 0010 (hexadecimal digit 2) is converted to 011 0010 (ASCII-code for '2'). Another example: binary 1011 (hexadecimal digit B) is converted to 100 0010 (ASCII-code for 'B') Make sure that your subroutine is documented according to our requirements.
HA 3.2. Using the labwork project in the Nios II IDE, create a new file called hexasc.s
HA 3.3. In the file hexasc.s, write a subroutine called hexasc, that converts a 4-bit hexadecimal value to the corresponding 7-bit ASCII-code.
I've drawn a flowchart for the program:
And the program I've tried is this but I suspect that it is not according to spec:
.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
Can you help me develop and solve this assignment? There's plenty of time, it's not due until in a month.
Update
After seeing here in a comment that the flowchart was incorrect, I've made the necessary adjustments:
I also want to discuss how this algorithm is correct that converts between hexadecimal and ASCII.
Update / edit
Here is the complete program.
.global hexasc
.text
.align 2
hexasc: movi r8, 0x09
movi r9, 0x0f
andi r4, r4, 0x0f #keep only 4 bits
bgt r4, r8, L1 #is x>9?
movi r2, 0x30
add r2, r2, r4
andi r2, r2, 0xff
ret
L1: movi r2, 0x37
add r2, r2, r4
andi r2, r2, 0xff
ret