Jump to content

Assembly code problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Fredrik373

Fredrik373

    Newbie

  • Members
  • Pip
  • 7 posts
Hello again!

I am supposed to convert a 4-bit hexadecimal value to the corresponding 7-bit ASCII-code.

These are the specifications:

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. 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 have tried to write a code but when I run it it does not work..

Anyone know what I have done wrong?

I am using Nios II.

Here is my code:

.global main hexasc

.text
.align 2

main: movi r8, 0x09
movi r9, 0x0f

andi r4, r4, 0x0f

bgt r8, r4, L1

movi r2, r2, 0x1e
add r2, r2, r4
andi r2, r2, 0xff

movia r2,putchar
br L2

L1: movi r2, r2, 0x29
add r2, r2, r4
andi r2, r2, 0xff

movia r2,putchar

L2: .end

#2
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
When converting from hex to ascii I usually use a lookup table type approach.

so I'll setup a lookup table similiar to this
'0123456789ABCDEF'
Now I'll pull off an entire byte of hex at a time which is going to equate to 0-15 and then I use that as an index into the lookup table.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
If the lookup table isn't allowed, you could also take advantage of the fact that the ASCII characters for '0'-'9' are consecutive, as are 'A'-'F'. So you could do something like:
if( r4 < 10 )
    r2 = r4 + '0';
else
    r2 = r4 - 10 + 'A';
// 'A' and '0' are the numeric ASCII codes for their
// corresponding characters.

Edited by dargueta, 15 February 2010 - 11:43 PM.
Off by one.

sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users