hi guys

im working on making a program where u get given a machine code. The first 6 bits are the opcode, the next 5 bits is the source register, the next 5 bits is destination register and the last 16bits is the immediate.

What we have to do is take the value of the immediate and add it to the value on the source register, which is given to us. I know how to isolate the 16bits on the 32bit instruction, by shitfting it 16bits left the shifting it 16bits right again. But apparently i have to sign extend this number coz im adding to a 32 bit register which is the source register. Any ideas on how to do this? Very confused..

thanks

I know how to isolate the 16bits on the 32bit instruction, by shitfting it 16bits left the shifting it 16bits right again.

that's not how you do it. AND it with a bitmask 0x00FF to get the lower 16 bits out of the instruction and copied into the destination register

But apparently i have to sign extend this number coz im adding to a 32 bit register which is the source register. Any ideas on how to do this? Very confused.

the MSB (bit 15) is the sign bit. If bit 15 == 1, then OR all ones into bits 16-31 using the bitmask 0xFF00 , otherwise leave it alone since it already has zeros there

>AND it with a bitmask 0x00FF to get the lower 16 bits...
It's one-byte mask... Where are 16 bits?..
>If bit 15 == 1, then OR all ones into bits 16-31 using the bitmask 0xFF00...
It's one-byte mask too...
?

commented: glad you were paying attention :P +8

ah... yes, you're right. i gave a 16bit value for a bitmask, when i meant to give a 32-bit value

the bitmasks should be 0x0000FFFF and 0xFFFF0000 , respectively

thanks for noticing.

sorry for the confusion

.

Hi again,

I haven't really studied anything about bit masks yet is there any other way to do it? I know wat u said about bit 15 == 1, but to do this did u store the 16bit immediate in a array? and then simply making immediate[16] - immediate[32] = 1?

what i've been doing so far is shitfing the hexadecimal input right by 26 bits to get the opcode, shifting the input number left by 6 then right by 27 to get the source register and those values have worked out pretty well so far.

Thanks again

that's ok i got how to do it now.. what i did was a bit different. I shifted the input 16bits left then 16bits right to get the 16 bit immediate, then i checked if the first bit was 1, and if it was I went immediate = -2^15 + ((immediate << 17) >> 17).

This is because the program kept reading the immediate as a unsigned value even though i declared immediate as a signed int. Would this still count as sign extending it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.