操作系统开发 – 婴儿教程 3
来源:互联网 发布:移动搜索引擎seo 编辑:程序博客网 时间:2024/04/29 15:10
A look at machine code (opcodes, prefix, etc)
; nasmw encode.asm -f bin -o encode.bin mov cx, 0xFFtimes 510-($-$$) db 0db 0x55db 0xAA
Don't partycopy to disk. Just open this in DEBUG (for Windows, Hexdump will be nice for Linux users)
C:\osdev\debug encode.bin
Type in 'd' after the '-' to see the binary file. ('?' will give you help, 'q' will quit). You will see something like this:
0AE3:0100 B9 FF 00 00 00 00 etc...
Look up the opcode for MOV here: http://www.baldwin.cx/386htm/MOV.htm See Section "17.2.2.1 Opcode" here: http://www.baldwin.cx/386htm/s17_02.htm
In other words, there is a unique register number (CX=1) added to the base opcode value 'B8' to give 'B9', which you see in the dump.
But watch what happens when you replace CX with ECX:
mov ecx, 0xFFtimes 510-($-$$) db 0db 0x55db 0xAA
0AE3:0100 66 B9 FF 00 00 00 00 etc...
The '66' is an Operand Size Override Prefix generated by the assembler when there is a discrepancy with the default mode, which when NASM assembles binary files, it is 16-bit. The same thing happens if you use the BITS directive to change the mode, but it differs from the size of the operand:
[BITS 32] mov cx, 0xFF times 510-($-$$) db 0 db 0x55 db 0xAA
This doesn't actually change the mode of the processor, but it does help it interpret the subsequent bytes.
Addresses
Address encoding is a bit more complicated
mov cx, [temp] temp db 0x99 times 510-($-$$) db 0 db 0x55 db 0xAA
0AE3:0100 8B 0E 04 00 99 00 00 00 etc...
- '8B' is the opcode
- '0E' is a ModR/M byte which help the opcode interpretation
See Section "17.2.1 ModR/M and SIB Bytes" here: http://www.baldwin.cx/386htm/s17_02.htm
The rules for interpreting this byte, which contains different fields (see Fig. 17-2), but fortunately Table 17-2 makes it easier. Look up '0E' and you will see at the left it says "disp16" which means that the operand will be interpreted as a 16-bit offset.
'04 00' is the 16-bit offset. If you are confused why 0x0004 is backwards, it's because the Intel processor is "little endian". The "little" end of the number comes first.
'99' is of course the value of the byte at 0x0004 (8B is at 0x0000)
Be aware of another prefix called the Address size Override Prefix '67' which the assembler generates when there is a discrepancy just like with '66' above.
This stuff matters for a bunch of reasons, but since we will be making the switch from 16-bit real mode to 32-bit protected mode, our code is going to also change. And being aware of what a dump looks like can prevent a lot of grief.
- 操作系统开发 – 婴儿教程 3
- 操作系统开发 – 婴儿教程 1
- 操作系统开发 – 婴儿教程 2
- 操作系统开发 – 婴儿教程 4
- 操作系统开发 – 婴儿教程 5
- 操作系统开发 – 婴儿教程 6
- 操作系统开发 – 婴儿教程 7
- 操作系统开发 – 婴儿教程 8
- Google 手机操作系统 Android 开发教程 转载
- CentOS5.3操作系统安装教程
- 简明教程:如何在Solaris操作系统上开发C 应用程序
- CentOS5.3 LINUX操作系统安装图文教程
- ROS机器人操作系统中级教程 3
- android开发-3设置操作系统
- CentOS操作系统安装教程
- CentOS操作系统安装教程
- CentOS操作系统安装教程
- 【分享】操作系统 编写教程
- 操作系统开发 – 婴儿教程 2
- 做淘宝客要懂得如何去分析竞争对手
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器
- Web应用界面设计规范—给项目组培训
- Android系统的开机画面显示过程分析
- 操作系统开发 – 婴儿教程 3
- 【研发管理】从技术到管理,思维转变是关键
- oracle查询语句中select from where group by having order by的解释与应用
- 操作系统开发 – 婴儿教程 4
- 操作系统开发 – 婴儿教程 5
- BNU - Consecutive Sums 公式与素数
- 网络经济与企业管理总结
- Android系统匿名共享内存(Anonymous Shared Memory)C++调用接口分析
- net中SessionState相关配置