Implemented infinite loops
This commit is contained in:
parent
121f77fe76
commit
b6722c5482
9 changed files with 198 additions and 113 deletions
|
@ -44,14 +44,32 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
|
|||
|
||||
case CALL:
|
||||
code = x64.Call(code, 0x00_00_00_00)
|
||||
size := 4
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := len(code)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
Position: Address(len(code) - 4),
|
||||
Position: Address(len(code) - size),
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination := labels[label.Name]
|
||||
distance := int32(destination) - int32(nextInstructionAddress)
|
||||
distance := destination - nextInstructionAddress
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
|
||||
case JUMP:
|
||||
code = x64.Jump8(code, 0x00)
|
||||
size := 1
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination := labels[label.Name]
|
||||
distance := destination - nextInstructionAddress
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
|
@ -67,8 +85,22 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
|
|||
// dataStart := config.BaseAddress + config.CodeOffset + Address(len(code))
|
||||
|
||||
for _, pointer := range pointers {
|
||||
slice := code[pointer.Position : pointer.Position+4]
|
||||
binary.LittleEndian.PutUint32(slice, pointer.Resolve())
|
||||
slice := code[pointer.Position : pointer.Position+Address(pointer.Size)]
|
||||
address := pointer.Resolve()
|
||||
|
||||
switch pointer.Size {
|
||||
case 1:
|
||||
slice[0] = uint8(address)
|
||||
|
||||
case 2:
|
||||
binary.LittleEndian.PutUint16(slice, uint16(address))
|
||||
|
||||
case 4:
|
||||
binary.LittleEndian.PutUint32(slice, uint32(address))
|
||||
|
||||
case 8:
|
||||
binary.LittleEndian.PutUint64(slice, uint64(address))
|
||||
}
|
||||
}
|
||||
|
||||
return code, data
|
||||
|
|
|
@ -44,6 +44,16 @@ func (a *Assembler) Call(name string) {
|
|||
})
|
||||
}
|
||||
|
||||
// Jump jumps to a position that is identified by a label.
|
||||
func (a *Assembler) Jump(name string) {
|
||||
a.Instructions = append(a.Instructions, Instruction{
|
||||
Mnemonic: JUMP,
|
||||
Data: &Label{
|
||||
Name: name,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Return returns back to the caller.
|
||||
func (a *Assembler) Return() {
|
||||
a.Instructions = append(a.Instructions, Instruction{Mnemonic: RETURN})
|
||||
|
|
|
@ -9,6 +9,7 @@ const (
|
|||
SYSCALL
|
||||
LABEL
|
||||
CALL
|
||||
JUMP
|
||||
)
|
||||
|
||||
// String returns a human readable version.
|
||||
|
@ -28,6 +29,9 @@ func (m Mnemonic) String() string {
|
|||
|
||||
case CALL:
|
||||
return "call"
|
||||
|
||||
case JUMP:
|
||||
return "jump"
|
||||
}
|
||||
|
||||
return "NONE"
|
||||
|
|
|
@ -8,5 +8,6 @@ type Address = uint32
|
|||
// Resolve: The function that will return the final address.
|
||||
type Pointer struct {
|
||||
Position Address
|
||||
Size uint8
|
||||
Resolve func() Address
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue