Question

    .text
    .align 2
    .global main
    .equ val,0x4712         # 16-bit binary code for 0x4712: 0100 0111 0001 0010
                            # Program code starts now
main:                          
        movi r16,val
        movi r17,0
loop:   addi r17,r17,1
        subi r16,r16,1 
        bne  r16,r0,loop
stop:   br   stop

How should I do to translate the above to machine code? I need to know how to make the translation besides the actual code. I figure I could try and get the opcodes for the instructions but the movi is a pseudoinstruction and I don't know where I can get it. Should I read it in the Nios II manual?

Update

The first four instructions are type immediate so that field form should be used. movi and subi both are pseudoinstruktions implemented in addi so the opcode for addi will be used. I was helped and I know that the instruction movi r16, val will translate to

00000100000100011100010010000100

so the opcode is 000100 binary which is 0x04 in hex base which the manual also states is the opcode for addi. So I think we have the first four opcodes, they are all 000100.

Update 2

I think I know the opcode and the immediate field of most of the instructions now:

The sequence 0100011100010010 is 0x4712 which is the variable val that was declared with .equ, So the first four opcodes should be 000100 since they're all addi and addi says it's 0x04. How to translate the two five-bit fields for the registers I don't know right now but could be checking with the manual. It says 'brhas opcode 0x06 so it should say 000110 in the opcode for br.bne` has opkod 0x1E which binary is 011110 = 30 (?)

Is this a correct beginning?

Was it helpful?

Solution

First off, why dont you at least try using an assembler to see what is produced?

You need to read the mips instruction set references to see the real mips instructions, often the pseudo instructions are described in these references. as far as movi goes, load upper is a bit obvious for controlling the upper bits, and either and or or or is an obvious way to set the lower bits. If the value is a small number then you only need and or or and use r0 as one of the operands (to zero the upper bits).

The problem with these pseudo instructions in mips is that you have to be careful not to use one in a branch shadow (unless it translates to a single instruction). I recommend learning an assembly language first using none of the pseudo instructions, then later to make your life easier, once you understand the instruction set and rules, then use pseudo instructions to make the code more readable or maintainable, etc. Personally I tend to stay with the pure instructions, giving a one-to-one experience (asm instruction to machine instruction).

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