Search code examples
pseudocodemachine-code

Translating pseudocode into machine code


For academic purposes, I am being asked to translate this statement

assign x the value 5

Into a machine code that is made up by an author of a computer science book, called brookshear machine code. I am given a hint that is

(HINTS: Assume that the value of x is to be stored into main memory location 47. Your program would begin by loading a value into a register. You do not need to specify the memory locations of your program. Don't forget to end the program with the HALT instruction.)

I am wondering if anyone knows the best way to approach this? He makes it clear to end with the halt instruction but I am unsure what exactly I should be doing.

0iii - No-operation
1RXY - Load register R with contents of location XY
2RXY - Load register R with value XY
3RXY - Store contents of register R at location XY
4iRS - Move contents of register R to register S
5RST - Add contents of registers S and T as binary numbers, place result in register R
6RST - Add contents of registers S and T as floating-point numbers, place result in register R
7RST - OR together the contents of registers S and T , place result in register R
8RST - AND together the contents of registers S and T , place result in register R
9RST - XOR together the contents of registers S and T , place result in register R
ARiZ - Rotate the contents of register R one bit to the right, Z times
BRXY - Jump to instruction at XY if contents of register R equal contents of register 0
Ciii - Halt
DRXY - Jump to instruction at XY if contents of register R are greater than contents of register 0
R,S,T - Register numbers
XY - A one-byte address or data value
Z - A half-byte value
i - Ignored when the instruction is de-coded: usually entered as 0

Above is the machine language I am expected to use.


Solution

  • If only there were an instruction:

     EABXY - Store value XY at location AB
    

    If that command existed, your program would be:

     E4705   # store '05' at address '47'
     C000    # halt
    

    But, that instruction doesn't exist -- partly because it takes five half-byte characters, and the instructions are meant to fit into four.

    So you're going to have to simulate the 'E' instruction using two steps.

    You can't specify a value to put into an address directly.

    • There is one instruction that lets you specify a value and put it somewhere.
    • There is one instruction that copies a value from somewhere, into an address

    That's really enough clues.