-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (52 loc) · 1.19 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/yamatcha/x86Emulator/emulator"
)
func dumpRegisters(emu *emulator.Emulator) {
for i := 0; i < emulator.RegistersCount; i++ {
fmt.Printf("%s = %08x\n", emulator.RegistersName[i], emu.Registers.GetRegister32(byte(i)))
}
fmt.Printf("EIP = %08x\n", emu.Eip)
}
func main() {
quiet := flag.Bool("q", false, "Quiet mode enabled?")
flag.Parse()
argv := flag.Args()
if len(argv) < 1 {
fmt.Println("few arguments")
os.Exit(1)
}
filename := argv[0]
emu := emulator.NewEmulator(emulator.MemorySize, 0x7c00, 0x7c00)
if emu == nil {
fmt.Println("Error: Value of eip or esp is invalid")
os.Exit(1)
}
binary, err := os.Open(filename)
if err != nil {
panic(err)
}
defer binary.Close()
mem, err := ioutil.ReadAll(binary)
emu.Memory = append(emu.Memory[:0x7c00], mem...)
for emu.Eip < emulator.MemorySize {
var code byte = emulator.GetCode8(emu, 0)
if !*quiet {
fmt.Printf("EIP = %X, Code = %02X\n", emu.Eip, code)
}
operator, err := emulator.Instructions(code)
if err != nil {
panic(err)
}
operator(emu)
if emu.Eip == 0x00 {
fmt.Printf("\n\n End of program.\n\n")
break
}
}
dumpRegisters(emu)
}