Skip to content

Commit

Permalink
Fix leaq corner cases and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
DQNEO committed Jul 12, 2023
1 parent c2fcb79 commit d954c42
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 12 additions & 3 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,19 @@ func encode(stmt *Stmt, keySymbol string, srcOp Operand, trgtOp Operand) (code [
var modRM, rm, reg byte
var displacementBytes []byte
if displacement == 0 {
mod := ModIndirectionWithNoDisplacement
reg = trgtRegi.toBits()
rm = srcRegi.toBits()
modRM = composeModRM(mod, reg, rm)
if rm == regBits("sp") {
mod := ModIndirectionWithNoDisplacement
reg = trgtRegi.toBits()
modRM = composeModRM(mod, reg, rm)
// There is no displacementBytes. For optimization ?
} else {
// same as isInInt8Range case below
mod := ModIndirectionWithDisplacement8
reg = trgtRegi.toBits()
modRM = composeModRM(mod, reg, rm)
displacementBytes = Bytes(0)
}
} else if isInInt8Range(int(displacement)) {
mod := ModIndirectionWithDisplacement8
reg = trgtRegi.toBits()
Expand Down
4 changes: 3 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func TestEncodeStringAsText(t *testing.T) {

{"callq SYMBOL", "callq myfunc", []byte{0xe8, 0, 0, 0, 0}},

{"leaq IND, REG", "leaq 127(%rbp), %rdi", []byte{0x48, 0x8d, 0b01_111_101, 0x7f}},
{"leaq IND8, REG", "leaq 127(%rbp), %rdi", []byte{0x48, 0x8d, 0b01_111_101, 0x7f}},
{"leaq IND0, REG", "leaq 0(%rbp), %rdi", []byte{0x48, 0x8d, 0b01_111_101, 0}},
{"leaq IND0, REG", " leaq 0(%rsp), %rdi", []byte{0x48, 0x8d, 0b00_111_100, 0b00_100_100}},
{"movb REG, IND", "movb %bl, 0(%rdi)", []byte{0x88, 0b00_011_111}},
{"movw REG, IND", "movw %bx, 0(%rdi)", []byte{0x66, 0x89, 0b00_011_111}},
{"movl IMM32, REG", "movl $2147483647, %ebx", []byte{0xb8 + 0b011, 0xff, 0xff, 0xff, 0x7f}},
Expand Down

0 comments on commit d954c42

Please sign in to comment.