NIOS II Assembly, How to set 2 different variables using one register location

What I am trying to accomplish is setting a decimal value to variables A and B. The decimal value is determined based on an input given by an external DIP switch. The DIP has 8 switches and the first switches 1-4 are supposed to control the A variable while switches 5-8 control the B variable. So I understand how to set a register value using the JP1 ports on my DE0 NANO Board, but dont know how to separate the hexi-decimal register value into two separate values. For example, if I switch on switch 1 and 8 i get the hexi value of 0xFFFFFF81 stored in the register. If i wanted A to be the decimal value 3 and B to also be 3 I would do this "A[3:0]<-DIP[1:4] and C[3:0]<-DIP[5:8]".

So to sum up what I said above how do i separate the hexi value stored into a register into two variables, where the first 4 switches control A and the second 4 control B?

Below is the code I have so far controlling the ports and LEDs (using the on board LEDs to confirm the swithces are working). Thanks in advance.

.equ ANSWER, 0x00001000
.equ LEDS, 0x10000010
.equ JP1_PORTS, 0x10000060
.global _start
_start: movia r2, ANSWER
        movia r3, LEDS
        movia r4, JP1_PORTS

loop: ldwio r5, 0(r4)
      stwio r5, 0(r3)
      br loop

DIP Switch


  • I don't know the instruction set for the NIOS II, but it almost certainly has bitwise and and shift instructions. For example, in C you would do:

    int sw = read_switch(); //lower byte = bbbbaaaa
    int a = sw & 0xF; //so now a= 0x0000aaaa
    int b = sw >> 4;  //b = xxxxbbbb, where x is whatever is in the upper bits. 
                      //If you know the upper bits are always 0, you can stop here
    b = b & 0xF       //b = 0000bbbb

    In something like ARM assembly (again, don't know NIOS, but it shouldn't be too too different), it'd look like this

    ldr r0, [r5]      ; r5=pointer to switch register, so r0 = b'bbbbaaaa'
    and r1, r0, #0xF  ; r1 is your A switches
    shr r2, r0, #4   
    and r2, r2, #0xF  ; r2 is your B switches