Implemented infinite loops

This commit is contained in:
2024-06-24 11:00:32 +02:00
parent dd495fab4e
commit 41f5dcbe62
9 changed files with 198 additions and 113 deletions

View File

@ -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