Fixed jump address calculation
This commit is contained in:
@ -57,23 +57,25 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
code = x64.Call(code, 0x00_00_00_00)
|
||||
size := 4
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, &Pointer{
|
||||
pointer := &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
}
|
||||
|
||||
if !exists {
|
||||
panic("unknown call label")
|
||||
}
|
||||
pointer.Resolve = func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
|
||||
distance := destination - nextInstructionAddress
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
if !exists {
|
||||
panic("unknown jump label")
|
||||
}
|
||||
|
||||
distance := destination - (pointer.Position + Address(pointer.Size))
|
||||
return Address(distance)
|
||||
}
|
||||
|
||||
pointers = append(pointers, pointer)
|
||||
|
||||
case COMMENT:
|
||||
continue
|
||||
@ -106,23 +108,25 @@ func (a Assembler) Finalize() ([]byte, []byte) {
|
||||
|
||||
size := 1
|
||||
label := x.Data.(*Label)
|
||||
nextInstructionAddress := Address(len(code))
|
||||
|
||||
pointers = append(pointers, &Pointer{
|
||||
pointer := &Pointer{
|
||||
Position: Address(len(code) - size),
|
||||
OpSize: 1,
|
||||
Size: uint8(size),
|
||||
Resolve: func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
}
|
||||
|
||||
if !exists {
|
||||
panic("unknown jump label")
|
||||
}
|
||||
pointer.Resolve = func() Address {
|
||||
destination, exists := labels[label.Name]
|
||||
|
||||
distance := destination - nextInstructionAddress
|
||||
return Address(distance)
|
||||
},
|
||||
})
|
||||
if !exists {
|
||||
panic("unknown jump label")
|
||||
}
|
||||
|
||||
distance := destination - (pointer.Position + Address(pointer.Size))
|
||||
return Address(distance)
|
||||
}
|
||||
|
||||
pointers = append(pointers, pointer)
|
||||
|
||||
case LABEL:
|
||||
labels[x.Data.(*Label).Name] = Address(len(code))
|
||||
@ -269,8 +273,16 @@ restart:
|
||||
following.Position += offset
|
||||
}
|
||||
|
||||
code = append(left, jump...)
|
||||
code = append(code, right...)
|
||||
for key, address := range labels {
|
||||
if address > pointer.Position {
|
||||
labels[key] += offset
|
||||
}
|
||||
}
|
||||
|
||||
code = make([]byte, len(left)+len(jump)+len(right))
|
||||
copy(code, left)
|
||||
copy(code[len(left):], jump)
|
||||
copy(code[len(left)+len(jump):], right)
|
||||
goto restart
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user