当前位置: 首页 > news >正文

游戏网站规划方案招聘网站建设维护

游戏网站规划方案,招聘网站建设维护,虚拟云手机 免费,网站建设期间注意事项文章目录mov 指令解码jmp 指令解码这篇文章举例来讲讲 mov 指令和 jmp 指令解码函数的实现,其他的指令解码函数都与这些类似。mov 指令解码 以 mov 指令中的一类:寄存器/内存 到/从 寄存器,来详细说明解码函数的实现。 机器指令格式如下&am…

文章目录

    • mov 指令解码
    • jmp 指令解码

这篇文章举例来讲讲 mov 指令和 jmp 指令解码函数的实现,其他的指令解码函数都与这些类似。

mov 指令解码

以 mov 指令中的一类:寄存器/内存 到/从 寄存器,来详细说明解码函数的实现。

机器指令格式如下:
在这里插入图片描述
各字段的含义如下:

w 1 bit  w=0表示数据宽度是字节,w=1表示数据宽度是字
d 1 bit   d=0表示reg是源操作数,d=1表示reg是目的操作数
reg 3 bitsREG W=0 W=1000 AL AX001 CL CX010 DL DX011 BL BX100 AH SP101 CH BP110 DH SI111 BH Dl
mod 2 bits00 MemoryMode,nodisplacement follows01 MemoryMode,8-bit displacementfollows10 MemoryMode,16-bit displacementfollows11 RegisterMode(no displacement)
rm 3 bitsMOD=11              EFFECTIVE ADDRESS CALCULATIONR/M w=0 w=1   R/M  MOD=00        MOD=01          MOD=10000 AL AX     000 (BX)+(SI)     (BX)+(SI)+D8    (BX)+(SI)+D16001 CL CX     001 (BX)+(DI)     (BX)+(DI)+D8    (BX)+(DI)+D16010 DL DX     010 (BP)+(SI)     (BP)+(SI)+D8    (BP)+(SI)+D16011 BL BX     011 (BP)+(DI)     (BP)+(DI)+D8    (BP)+(DI)+D16100 AH SP     100 (SI)          (SI)+D8         (SI)+D16101 CH BP     101 (DI)          (DI)+D8         (DI)+D16110 DH SI     110 DIRECTADDRESS (BP)+D8         (BP)+D16111 BH DI     111 (BX)          (BX)+D8         (BX)+D16

解码函数 decodeMovRegOrMemoryToFromReg 的目的就是将它转换为如下形式的中间指令格式:

指令类型,指令详细类型,[源操作数],[目的操作数]

decodeMovRegOrMemoryToFromReg 函数首先检测输入的机器指令的长度:

func decodeMovRegOrMemoryToFromReg(instructions []byte) []byte {/* 0b100010dw,mod reg r/m, (DISP-LO ), (DiSP-HI)*/decodegth := len(instructions)if decodegth < 2 {return nil}dispLen := lenDisplacement(instructions[1])if decodegth < 2+dispLen {return nil}....
}

由上文机器指令格式可知,这个指令至少有 2 字节长。如果长度达到 2 字节,再调用 lenDisplacement 获取偏移量的长度。如果指令长度达不到指令格式的要求,说明不是一条完整的指令,那就返回 nil。

lenDisplacement 的实现如下:

/* mod xxx r/m, (DISP-LO ), (DiSP-HI) */
func lenDisplacement(secondByte byte) int {mod := (secondByte & 0b11000000) >> 6rm := secondByte & 0b111if mod == 0b11 {return 0}if mod == 0b00 { /* mov bx, [1]*/if rm == 0b110 {return 2}return 0}if mod == 0b01 {return 1}return 2
}

就是根据 mod 和 rm 字段的含义返回偏移量的长度。

然后就是根据 d、w、mod、rm、reg 字段的含义,确定指令详细类型和操作数,将中间形式的指令格式返回:

	decodedInstructions := []byte{InstructionMov}// return 2 + dispLend := (instructions[0] & 0b10) >> 1w := instructions[0] & 0b1mod := (instructions[1] & 0b11000000) >> 6reg := (instructions[1] & 0b111000) >> 3rm := instructions[1] & 0b111switch mod {case 0b11: //RegisterMode(no displacement)if w == 0 {decodedInstructions = append(decodedInstructions, MovReg8ToReg8)} else {decodedInstructions = append(decodedInstructions, MovReg16ToReg16)}if d == 0 { //reg是源操作数decodedInstructions = append(decodedInstructions, reg)decodedInstructions = append(decodedInstructions, rm)} else {decodedInstructions = append(decodedInstructions, rm)decodedInstructions = append(decodedInstructions, reg)}default:if d == 0 {if w == 0 {decodedInstructions = append(decodedInstructions, MovReg8ToMemory)} else {decodedInstructions = append(decodedInstructions, MovReg16ToMemory)}decodedInstructions = append(decodedInstructions, reg)decodedInstructions = append(decodedInstructions,decodeMemoryOperand(mod, rm, instructions[2:])...)} else {if w == 0 {decodedInstructions = append(decodedInstructions, MovMemoryToReg8)} else {decodedInstructions = append(decodedInstructions, MovMemoryToReg16)}decodedInstructions = append(decodedInstructions,decodeMemoryOperand(mod, rm, instructions[2:])...)decodedInstructions = append(decodedInstructions, reg)}}return decodedInstructions

比如,当 mod 字段为 0b11 时,表示两个操作数都是寄存器,如果 d 为 0,那么 reg 字段就是源操作数,rm 字段就是目的操作数。如果 w 为1,那么操作数的宽度就是16位。这时候生成的中间指令格式为:

InstructionMov,MovReg16ToReg16 ,reg , rm

如果 reg 的值是 0,rm 的值是 1,这条指令的源汇编指令就是:

mov ax,cx

就是这么简单。

再看下解码 mov 立即数到内存/寄存器 解码函数 decodeMovImmediateToRegOrMemory 的实现:

func decodeMovImmediateToRegOrMemory(instructions []byte) []byte {/*1100011w, mod 000 rm, [disp-lo] [disp-hi] data [data]*/decodegth := len(instructions)if decodegth < 2 {return nil}w := instructions[0] & 0x1dispLen := lenDisplacement(instructions[1])dataLen := 1if w == 1 {dataLen = 2}if decodegth < 2+dispLen+dataLen {return nil}decodedInstructions := []byte{InstructionMov}mod := (instructions[1] & 0b11000000) >> 6rm := instructions[1] & 0b111if w == 0 {if mod == 0b11 {decodedInstructions = append(decodedInstructions, MovImmediateToReg8)decodedInstructions = append(decodedInstructions, instructions[2])decodedInstructions = append(decodedInstructions, rm)} else {decodedInstructions = append(decodedInstructions, MovImmediate8ToMemory)decodedInstructions = append(decodedInstructions, instructions[decodegth-1])decodedInstructions = append(decodedInstructions,decodeMemoryOperand(mod, rm, instructions[2:decodegth-1])...)}} else {if mod == 0b11 {decodedInstructions = append(decodedInstructions, MovImmediateToReg16)decodedInstructions = append(decodedInstructions, instructions[2])decodedInstructions = append(decodedInstructions, instructions[3])decodedInstructions = append(decodedInstructions, rm)} else {decodedInstructions = append(decodedInstructions, MovImmediate16ToMemory)decodedInstructions = append(decodedInstructions, instructions[decodegth-2])decodedInstructions = append(decodedInstructions, instructions[decodegth-1])decodedInstructions = append(decodedInstructions,decodeMemoryOperand(mod, rm, instructions[2:decodegth-2])...)}}return decodedInstructions
}

其他的都类似。

jmp 指令解码

jmp 指令包含直接转移和条件转移。
decode_jmp.go 中先把所有的指令详细类型定义出来:

const (//非条件转移JmpNotShort           uint8 = iota //16位IP偏移量JmpShort                           //8位IP偏移量JmpDirectIntersegment              //cs 16位,IP 16位JmpReg16                           //IP的值在寄存器中JmpIndirectWithinsegmentJmpIndirectIntersegment//条件转移JmpJoJmpjnoJmpJbJmpJnbJmpJeJmpJneJmpJbeJmpJnbeJmpJsJmpJnsJmpJpJmpJnpJmpJlJmpJnlJmpJleJmpJnleJmpJcxz
)

初始化函数,注册所有的 jmp 指令与它的解码函数:

func init() {//jmpAddDecodeInstruction(0xE9, decodeJmpDirectWithinsegment)AddDecodeInstruction(0xEA, decodeJmpDirectIntersegment)AddDecodeInstruction(0xEB, decodeJmpDirectWithinsegmentShort)AddDecodeInstruction2(0xFF, 0b100, decodeJmpIndirectWithinsegment)AddDecodeInstruction2(0xFF, 0b101, decodeJmpIndirectIntersegment)var firstByte bytefor firstByte = 0x70; firstByte <= 0x7F; firstByte++ {AddDecodeInstruction(firstByte, decodeJmpConditional)}//jcxzAddDecodeInstruction(0xE3, decodeJmpConditional)}

以段内间接转移为例,它的机器指令格式如下:
在这里插入图片描述
对应的解码函数 decodeJmpIndirectWithinsegment 代码如下:

func decodeJmpIndirectWithinsegment(instructions []byte) []byte {/*11111111,mod 1 0 0 r/m,(DISP-LO ) (DISP-HI)*/decodegth := len(instructions)if decodegth < 2 {return nil}dispLen := lenDisplacement(instructions[1])if decodegth < 2+dispLen {return nil}decodedInstructions := []byte{InstructionJmp}mod := (instructions[1] & 0b11000000) >> 6rm := instructions[1] & 0b111if mod == 0b11 {decodedInstructions = append(decodedInstructions, JmpReg16)decodedInstructions = append(decodedInstructions, rm)} else {decodedInstructions = append(decodedInstructions, JmpIndirectWithinsegment)decodedInstructions = append(decodedInstructions,decodeMemoryOperand(mod, rm, instructions[2:])...)}return decodedInstructions
}

如果 mod 是 0b11,则返回的中间形式机器指令为:

InstructionJmp,JmpReg16,rm

条件转移的解码函数就更简单,因为条件转移的机器指令固定 2 个字节:
在这里插入图片描述
只需根据第一个字节确定详细指令类型即可,它的实现如下:

func decodeJmpConditional(instructions []byte) []byte {/*xxxxxxxx,IP-INC8*/if len(instructions) < 2 {return nil}table := map[uint8]uint8{0x70: JmpJo,0x71: Jmpjno,0x72: JmpJb,0x73: JmpJnb,0x74: JmpJe,0x75: JmpJne,0x76: JmpJbe,0x77: JmpJnbe,0x78: JmpJs,0x79: JmpJns,0x7A: JmpJp,0x7B: JmpJnp,0x7C: JmpJl,0x7D: JmpJnl,0x7E: JmpJle,0x7F: JmpJnle,0xE3: JmpJcxz,}return []byte{InstructionJmp, table[instructions[0]], instructions[1]}
}

其他指令的解码函数实现都类似。

后续文章讲解 EU 如何实现执行 mov,jmp 以及一些算数运算指令。

http://www.yayakq.cn/news/307486/

相关文章:

  • 宁波海曙网站开发公司电话微信网站建设方案ppt
  • ecilpse做网站wordpress的模板文件下载
  • 网站建设专业性的评价php做网站界面代码
  • 桂林北站附近景点关键词制作软件
  • 移动网站建设服务商品牌推广营销
  • 无经验可以做网站编辑吗云优cms
  • 手机网站建设规划书挂网站需要什么服务器
  • 做网站销售水果p2p金融网站开发
  • 线上网站开发系统流程图网站开发的相关技能
  • 平台网站建设公司随州网络科技有限公司
  • 西宁网站建设价格低做网站的公司重庆
  • 闽侯福州网站建设深圳松岗 网站建设
  • 一般做海报的图片跟哪个网站下载wordpress comment
  • 婚介网站建设的策划天眼查企业查询在线官网
  • 网站建设最低要求长春网站建设推广优化
  • 网站页面一般做多大wordpress清理过期文件
  • 婺源网站建设网站开发接口文档模板
  • 西安网站建设项目武侯区网站建设
  • 微网站是自己做可以不江西南昌网站建设招标
  • 广州建设外贸网站凡科免费建站
  • 自己做直播网站玉树wap网站建设
  • 景宁县建设局网站市场调研报告范文3000字
  • 没有网站如何做SEO推广有用吗网叶制作
  • 密云广州网站建设学校品牌建设
  • dedecms 门户网站国际局势最新消息今天
  • 网站开发所需要的书籍在长沙做网站需要多少钱
  • 私人服装定制网站用wordpress教程视频
  • 深圳市网站开发坂田附近商品推广软文写作500字
  • 口碑好网站建设价格网站主题页
  • 本地环境如何安装wordpress湖南优化公司