简体   繁体   中英

Assembly Language instruction into Machine language instruction

Reference : 参考 Question: 题

I'm not getting the same values as they do in their answer.

The opcode for ld (immediate) is 0x31 = 0011 0001 The value is 0x10A = 0001 0000 1010. I dont know how to account for the Ri register here.

The physical format for ld (0x31) is two words long.

Word 1: bits 7-2 are for the opcode. Therefore the binary sequence for ld gets shortened to 110001 bits

bits 1-0 are for Ri.

Word 2: all 8 bits reserved for value

10A = 0001 0000 1010 gets shortened to ... 0000 1010? What about the 0001? :(

My result - 1100 01 Ri 0000 1010.

Yeah the answer in the image is wrong, that's for 0x10D not 0x10A. Yours is wrong too.

For starters, ld has two versions, one for memory load and one for constant load. The one in the question is the memory load, and that has opcode 0x30 . Ri is just the register number for the operand, and that's clearly 1 here. Thus, the first byte looks like 1100 0001 (this is correct in the image). Then you just need to put the constant after it as two bytes, big endian.

So, the correct answer is 1100 0001 0000 0001 0000 1010

Looking at the documentation included in the question, there are several instructions that are being proposed. The machine has four 8 bit registers which are numbered R0, R1, R2, and R3 which are addressed using two bits.

The op codes for the instructions are specified as two hex digits in the documentation. However in the actual implementation, the op code area of the instructions are not two hex digits in size so you have to take the hex value and shift the value left by 2 bits. So an op code for a load of 0x30 (0011 0000) is left shifted resulting in 0xC0 (1100 0000 in binary) or the op code for a store of 0x32 (0011 0010) is left shifted resulting in 0xC8 (1100 1000) and the register number of zero to three is inserted into the lower two bits of the two hex digits.

The load from address instruction is ld Ri,xxxx where xx is a 16 bit address of the location containing the 8 bit value to load into the register. This load instruction sets the specified 8 bit register (R0, R1, R2, or R3) with the 8 bit value at the specified address.

The actual bit formats for the load instruction are: (1) op code (0x30) in the most significant 6 bits followed by (2) the register number (0 - 3) in the next two bits followed by (3) an 16 bit value that is an address to the value to load.

The ld R1,0x10A should then look like 1100 0001 0000 0001 0000 1010 in binary which separates out as 1100 00 as the op code 0x30, 01 as the register number R1, and 0000 0001 0000 1010 which is 0x010A.

The load immediate ld R3,$-12 should look like 1100 0111 1111 0400 as -12 is 0xfff4

The add R1,R3 should look like 0100 0001 1100 0000 assuming the unused bits are set to zero. This would be 010000 as the 0x10 op code for add, 01 for register R1 and 11 for register R3. The remaining 6 bits in the second 8 bit part of the add instruction are unused and ignored.

The sto R1,0x10B should look like 1100 1001 0000 0001 0000 1011 which is op code of 0x32, register 1, to be stored at the 16 bit address of 0x010B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM