-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
80 lines (69 loc) · 2.52 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def error(error):
"""Displays to stdout the parameter in error and exits with return code 1."""
print("pyasm: error: " + error)
exit(1)
def warning(warning):
"""Displays to stdout the warning in warning, but it doesn't terminate the program."""
print("pyasm: warning: " + warning)
###
##
# There are functions that do the same functions as other function
# This is to make code more understandable in pyasm.py
##
###
def cli(opcode):
"""Converts cli opcode into opcodes: Parameters:
\nOpcode: the opcode for cli: b\"\\xFA\"
"""
cli_opcode = bytearray(opcode)
return cli_opcode
def decrease(register):
"""Converts dec instruction into opcodes. Returns the bytes. \nParameters:
\nRegister: the opcode or opcodes for dec"""
dec_opcodes = bytearray(register)
return dec_opcodes
def hlt(opcode):
"""Converts hlt instruction into opcode. Returns the bytes. Parameters:
Opcode: the opcode for hlt"""
hlt_opcode = bytearray(opcode)
return hlt_opcode
def increase(register):
"""Converts inc instruction into opcodes. Returs the bytes. Parameters:
Register: the opcode for inc"""
inc_opcodes = bytearray(register)
return inc_opcodes
def interrupt(opcode, address):
"""Converts int instruction into opcodes. Returns the bytes.
\nParameters:
\n\nopcode: usually b\"\\xCD\"
\naddress: the interrupt number, in hexadecimal"""
try:
int_address_byte = bytearray(bytes([int(address, 16)]))
int_opcodes = bytearray(opcode + int_address_byte)
return int_opcodes
except ValueError:
print("Error")
return None
def jump(register, address):
"""Converts the jmp instruction into opcodes. Returns the bytes."""
try:
# it can be a byte, two or more
jmp_address_bytes = bytearray(bytes([int(address)]))
jmp_opcodes = bytearray(register + jmp_address_bytes)
return jmp_opcodes
except ValueError:
return None
def mov(register, address):
"""Converts the mov instruction into opcodes. Returns the bytes.
\nParameters:\n\nregister: the opcode register you want be the address moved
\naddress: the address that needs to be moved into the register
\nNote: the function converts the integer into hexadecimal and then to bytes."""
# number needs to be in hexadecimal, thats why there is a try statement
# if something goes wrong with the hex value it returns none
try:
mov_address_byte = bytearray(bytes([int(address, 16)]))
mov_opcodes = bytearray(register + mov_address_byte)
return mov_opcodes
except ValueError:
print("Error")
return None