In this task, an interpreter for GRAsm is to be implemented. This should execute a program in byte code correctly and recognize any errors that may occur. A virtual state on which the operations are to work is available as an abstraction for the "GRA machine".
The following "virtual" 64-bit registers are available:
Registers Special functionality
ip Instruction Pointer
ac Accumulator Register, Return Register
r0, r1, r2, r3, r4, r5, r6, r7 General Purpose registers
These are defined as:
struct grasm_state { uint64_t ip, acc, r0, .., r7; };
arranged in memory.
The following table shows the available instructions and encodings.
Instruktion Immediate Encoding Funktion
stop -- 0x01 Ends the program
nop -- 0x0f No function
set uint64_t 0x10 <imm> ac = imm
set rX uint64_t
cpy rX --
cpy rX rY --
add uint64_t
add rX uint64_t
add rX --
add rX rY --
sub uint64_t
sub rX uint64_t
sub rX --
sub rX rY --
mul uint64_t
mul rX uint64_t
mul rX --
mul rX rY --
xchg rX --
xchg rX rY --
and uint64_t
and rX uint64_t
and rX --
and rX rY --
or uint64_t
or rX uint64_t
or rX --
or rX rY --
xor uint64_t
xor rX uint64_t
xor rX --
xor rX rY --
not --
not rX --
not rX rY --
cmp rX uint64_t
0x19 <XY>
0x20 <imm>
0x29 <XY>
0x22 <imm>
0x2b <XY>
0x24 <imm>
0x2d <XY>
0x30 <imm>
0x33 <XY>
0x34 <imm>
0x37 <XY>
0x38 <imm>
0x3b <XY>
0x3c 0x3e <XY>
rX = imm ac = rX rX = rY
ac += imm
rX += imm
rX += rY ac -= imm rX -= imm
rX -= rY ac *= imm rX *= imm
rX *= rY
tmp = ac; ac = rX; rX = tmp tmp = rX; rX = rY; rY = tmp ac &= imm
rX &= imm
rX &= rY ac |= imm rX |= imm
rX |= rY ac ^= imm rX ^= imm
rX ^= rY
ac = ~ac
ac = ~rX
rX = ~rY
ac = rX - imm
Instruktion
cmp rX rY Immediate
-- Encoding
0x41 <XY> Funktion
ac = rX - rY
tst rX uint64_t 0x42 <0X> <imm> ac = rX & imm
tst rX rY -- 0x43 <XY> ac = rX & rY
shr uint8_t 0x50 <imm> ac >>= imm
shr rX uint8_t 0x51 <0X> <imm> rX >>= imm
shr rX -- 0x52 <0X> ac >>= rX
shr rX rY -- 0x53 <XY> rX >>= rY
shl uint8_t 0x54 <imm> ac <<= imm
shl rX uint8_t 0x55 <0X> <imm> rX <<= imm
shl rX -- 0x56 <0X> ac <<= rX
shl rX rY -- 0x57 <XY> rX <<= rY
ld uintptr_t 0x60 <imm> ac = [imm]
ld rX uintptr_t 0x61 <0X> <imm> rX = [imm]
ld rX -- 0x62 <0X> ac = [rX]
ld rX rY -- 0x63 <XY> rX = [rY]
st uintptr_t 0x64 <imm> [imm] = ac
st rX uintptr_t 0x65 <0X> <imm> [imm] = rX
st rX -- 0x66 <0X> [rX] = ac
st rX rY -- 0x67 <XY> [rX] = rY
go uintptr_t 0x70 <imm> ip = <imm>
go rX -- 0x71 <0X> ip = rX
gr int16_t 0x72 <imm> ip = ip + <imm>
jz uintptr_t 0x73 <imm> ip = ac == 0 ? <imm> : ip + sizeof(jz)
jz rX -- 0x74 <0X> ip = ac == 0 ? rX : ip + sizeof(jz)
jrz int16_t 0x75 <imm> ip = ac == 0 ? ip + <imm> : ip +
With the instruction ecall, (unknown) external functions are called at the given address: Be sure to follow the calling convention!
The functions accept a maximum of 6 parameters, the result should be stored in ac. Unless otherwise specified, all immediates are encoded in little-endian format.
i.e. add 0x01234567 -> 20 67 45 23 01 00 00 00 00.
Especially with the jumps, make sure you set the ip correctly!
ip is updated at the end of a (valid) instruction, i.e. after any error handling. Jumps set the ip explicitly.
Relative jumps are calculated relative to the current instruction.
The memory addresses passed for ld/st and ecall are valid and refer directly to the underlying memory.
The upper 4 bits of <0X> are dont-care for this task, so they should not be checked. Shifts can shift a maximum of 63 bits, i.e. only 7 bits of the operand are considered.
If errors occur, the following error codes should be returned:
error Error code reason
No error 0 --
unknown Opcode -1 Instruction does not exist or is incomplete Out-of-Bounds-access -2 ip >= len, Register not valid
Example program (line breaks, offsets and comments for visualization only):
TASK:
In x86-64 assembly, implement an interpreter that meets the above requirements; in particular, all instructions must be implemented.
Signature(for the registers passing from C to assembly): uint64_t grasm_interpreter(struct grasm_state* state, size_t len, uint8_t prog[len]);
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of