Question

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 
Was it helpful?

Solution

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

11111101 AND 01111111 => 01111101

Regarding I don't know how to return values, the answer is written in your assignment:

Return value: Only one, returned in register r2

Simply store whatever you want to return in register r2 before your routine ends.

OTHER TIPS

You might be over-thinking this assignment / assembly vs. other structured programming terminology for some of these points. In assembly (in general), a subroutine is just that - a snippet of code that can be run independently. One of the subroutines that I reused consistently throughout assembly, for instance, was a subroutine to take a string written to flash and send it over the serial connection (more or less, a constant string cout... you'll get to it soon enough). I personally wrote these subroutines using .macro directives, to allow a more 'procedural' feel to the code...

The return value is merely as the problem suggests:

Return value: Only one, returned in register r2. The 7 least significant bits in register r2 must be an ASCII code as described below.

This means that whatever the appropriate output of the program should be, it should be in register r2.

I would start by getting familiar with this document, from the standpoint of the actual coding stuff: http://www.altera.com/literature/hb/nios2/n2cpu_nii51017.pdf

Finally, when I took this class, we used an Atmel AVR processor, and this book, which is by far one of the most complete and easy to read 'beginners' books for assembly programming that I've ever seen... then again, I was lucky enough to have been in a class with the professor who wrote the book... and it also might not be as useful outside of the AVR instruction set, but it does give a very indepth, practical look at low level systems programming.

http://www.amazon.com/Some-Assembly-Required-Microcontroller-ebook/dp/B007IVDXVM/ref=cm_cr_pr_product_top

Typically, this is done by ANDing off the upper 28 bits and using what's left to index an '0123456789ABCDEF' ASCII lookup table. It's like 3 instructions in ARM.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top