Reputation: 215
how do we put the program instruction c = a-b; in machine code instructions? i've been provided with op-codes, memory address for a=80, b=81, c=82 and program counter = 30 hex. instructions are 16 bits long with 4bit op-code, 4bit for register and 16 bit for memory address.
i just need to know the formal way how it is started. this is because i really don't understand what my lecturer teaches. if i can get a clear direction, then i am confident i can do this without the help of my lecturer.
Upvotes: 0
Views: 1058
Reputation: 7309
The mnemonics would look something like this:
mov eax, [a] ; move 4 bytes starting at address a to the eax register
sub eax, [b] ; subtract 4 bytes starting at memory address b from the eax register
mov [c], eax ; move the contents of the eax register to the memory address of c
You substitute in your opcodes for the operations (mov
, sub
), the register address for (eax
), memory addresses for (a
, b
, c
) and the and then the result are actual machine instructions. I'm assuming here your numbers are 32 bit integers, so I'm using the eax register which is 32 bits long for an x86 processor, but the specifics of which register you use is arbitrary, as long as it's big enough to hold your number. The mov
instruction will read as many bytes as the register can hold starting at the memory address provided.
You don't explicitly need to do anything about the program counter for this example, the CPU increments the program counter as it executes each instruction.
Upvotes: 2