Cameron Furuseth
Cameron Furuseth

Reputation: 11

Turning assembly instruction into hex using C

I'm trying to make a baby assembler essentially using C. I have it reading in the instructions fine, but I'm struggling with taking the instruction and properly converting it into hex. How can I take multiple decimal values and put them together into a 32 bit binary number to convert to hex?

Here is what I have now for the instruction addi $t0,$t1,10:

opcode = opcode >> 2;
rs = rs >> 1;
rt = rt >>1;
imm = imm >> 15;
printf("0x%X:\t0x%X%X%X%X",list[i].a,opcode,rs,rt,imm);

Upvotes: 1

Views: 735

Answers (1)

MotKohn
MotKohn

Reputation: 3965

First or(|) it into one number. For example and this is for sure wrong. Just to get the idea.

int hex = (opcode<<24)|(rs<<16)|(rt<<8)|(imm);

Then:

printf("0x%X:\t0%X",list[i].a, hex);

Upvotes: 1

Related Questions