【说明】
前些天拜读《深入理解计算机系统》这本神作,读到第四章的时候,作者提出了一个迷你的计算机系统设计原理,称之为Y86 ,我觉得挺好玩的。就自己实现了这个Y86小系统,内容包含两个可执行文件,
一个是汇编器,运行后,可用指定汇编文件(Y86汇编码),来生成一个二进制内存映像,以及一个反汇编文件用于观察。
一个迷你计算机系统,可以一条一条读取二进制映像中的指令,解析执行,模拟指令对内存,寄存器以及状态位的改变。执行后打印迷你系统的状态,以及内存信息。
【下载地址】
我将源代码上传了,可以免费下载 http://download.csdn.net/detail/u013476840/6978643
写的时间不长,没怎么测试,可能存在一些小BUG,大家发现了给我留个言,我修改,谢谢啦
代码写得不是太好,大家多多包涵。。。。。
【使用示例】
首先在主目录下make 生成 2个重要的文件 一个是 AS 一个是y86_Sys
1】实例汇编文件test.S ( 来自《深入理解计算机系统》第四章)
# Execution begins at address 0
.pos 0
init: irmovl Stack, %esp # Set up stack pointer
irmovl Stack, %ebp # Set up base pointer
call Main # Execute main program
halt # Terminate program
# Array of 4 elements
.align 4
array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000
Main: pushl %ebp
rrmovl %esp,%ebp
irmovl $4,%eax
pushl %eax # Push 4
irmovl array,%edx
pushl %edx # Push array
call Sum # Sum(array, 4)
rrmovl %ebp,%esp
popl %ebp
ret
# int Sum(int *Start, int Count)
Sum: pushl %ebp
rrmovl %esp,%ebp
mrmovl 8(%ebp),%ecx # ecx = Start
mrmovl 12(%ebp),%edx # edx = Count
xorl %eax,%eax # sum = 0
andl %edx,%edx # Set condition codes
je End
Loop: mrmovl (%ecx),%esi # get *Start
addl %esi,%eax # add to sum
irmovl $4,%ebx #
addl %ebx,%ecx # Start++
irmovl $-1,%ebx #
addl %ebx,%edx # Count--40
jne Loop # Stop when 0
End: rrmovl %ebp,%esp
popl %ebp
ret
# The stack starts here and grows to lower addresses
.pos 0x100
Stack:
2】汇编器
执行
生成的文件中有一个.dis 是反汇编
| | # Execution begins at address 0
|0x00000: | .pos 0
|0x00000: 30f400010000 | init: irmovl Stack, %esp # Set up stack pointer
|0x00006: 30f500010000 | irmovl Stack, %ebp # Set up base pointer
|0x0000c: 8024000000 | call Main # Execute main program
|0x00011: 00 | halt # Terminate program
| | # Array of 4 elements
| | .align 4
|0x00014: 0d000000 | array: .long 0xd
|0x00018: c0000000 | .long 0xc0
|0x0001c: 000b0000 | .long 0xb00
|0x00020: 00a00000 | .long 0xa000
|0x00024: a05f | Main: pushl %ebp
|0x00026: 2045 | rrmovl %esp,%ebp
|0x00028: 30f004000000 | irmovl $4,%eax
|0x0002e: a00f | pushl %eax # Push 4
|0x00030: 30f214000000 | irmovl array,%edx
|0x00036: a02f | pushl %edx # Push array
|0x00038: 8042000000 | call Sum # Sum(array, 4)
|0x0003d: 2054 | rrmovl %ebp,%esp
|0x0003f: b05f | popl %ebp
|0x00041: 90 | ret
| |
| | # int Sum(int *Start, int Count)
|0x00042: a05f | Sum: pushl %ebp
|0x00044: 2045 | rrmovl %esp,%ebp
|0x00046: 501508000000 | mrmovl 8(%ebp),%ecx # ecx = Start
|0x0004c: 50250c000000 | mrmovl 12(%ebp),%edx # edx = Count
|0x00052: 6300 | xorl %eax,%eax # sum = 0
|0x00054: 6222 | andl %edx,%edx # Set condition codes
|0x00056: 7378000000 | je End
|0x0005b: 506100000000 | Loop: mrmovl (%ecx),%esi # get *Start
|0x00061: 6060 | addl %esi,%eax # add to sum
|0x00063: 30f304000000 | irmovl $4,%ebx #
|0x00069: 6031 | addl %ebx,%ecx # Start++
|0x0006b: 30f3ffffffff | irmovl $-1,%ebx #
|0x00071: 6032 | addl %ebx,%edx # Count--40
|0x00073: 745b000000 | jne Loop # Stop when 0
|0x00078: 2054 | End: rrmovl %ebp,%esp
|0x0007a: b05f | popl %ebp
|0x0007c: 90 | ret
| |
| | # The stack starts here and grows to lower addresses
|0x00100: | .pos 0x100
|0x00100: | Stack:
3】迷你虚拟机运行 test.bin