Why do the x86 instruction INC (increment) and DEC (decrement) not affect the CF (carry flag) in FLAGSREGISTER?

解决方案

To understand why you probably need to remember the current "x86" CPUs with 32 and 64 bit values started life as much more limited 8-bit machines, going back to the Intel 8008. (I coded in this world back in 1973, I still remember (ugh) it!).

In that world, registers were precious and small. You need INC/DEC for various purposes, the most common being loop control. Many loops involved doing "multi-precision arithmetic" (e.g, 16 bits or more!) By having INC/DEC set the Zero flag (Z), you could use them to control loops pretty nicely; by insisting the loop control instructions not change the Carry flag (CF), the carry is preserved across loop iterations and you can implement multiprecision operations without writing tons of code to remember the carry state.

This worked pretty well, once you got used to the ugly instruction set.

On more modern machines with larger word sizes, you don't need this is much, so INC and DEC could be semantically equivalent to ADD ...,1 etc. That in fact is what I use when I need the carry set :-}

Mostly, I stay away from INC and DEC now, because they do partial condition code updates, and this can cause funny stalls in the pipeline, and ADD/SUB don't. So where it doesn't matter (most places), I use ADD/SUB to avoid the stalls. I use INC/DEC only when keeping the code small matters, e.g., fitting in a cache line where the size of one or two instructions makes enough difference to matter. This is probably pointless nano[literally!]-optimization, but I'm pretty old-school in my coding habits.

My explanation tells us why INC/DEC set the Zero flag (Z). I don't have a particularly compelling explanation for why INC/DEC set the sign (and the Parity flag).

EDIT April 2016: It seems that the stall problem is handled better on modern x86s. See INC instruction vs ADD 1: Does it matter?

汇编inc为什么不影响cf,为什么INC和DEC指令不影响进位标志(CF)?相关推荐

  1. 为什么INC,DEC指令不影响CF标志位呢?

    引言 我们知道执行算数运算指令的时候,当结果的最高位与进位或者借位时会把状态寄存器FLAGS的CF位置为1,否则置为0,但是为什么INC和DEC这样的指令不影响CF标记位呢? 怀着对这个问题的疑惑,在 ...

  2. 汇编inc为什么不影响cf_为什么INC和DEC指令不会影响进位标志?

    为了理解为什么你可能需要记住当前具有32位和64位值的"x86"处理器,它们开始成为更受限制的8位机器,回溯到英特尔8008处理器. 1973年,我还记得(唉)!). 在那个世界上 ...

  3. dec-c 怎么改语言,汇编中的dec指令是什么指令

    公告: 为响应国家净网行动,部分内容已经删除,感谢读者理解. 话题:汇编中的dec指令是什么指令?回答:相当于c语言中的自减.如:dec R0就是说R0=R0-1.如果R0=5,那么运算过后,R0=4 ...

  4. 汇编指令sub也改变标志位

    SUB指令介绍: SUB:不带借位的减法指令. 指令格式:SUB OP1,OP2 指令功能:(OP1)←(OP1)-(OP2),将OP1-OP2的值,保存在OP1中,如:SUB [EAX],1 以EA ...

  5. 汇编SF、CF、 OF 、ZF、 PF等条件标志位

    CF:carry flag,进位标志,进位为1 SF:sign flag,符号标志,负号为1 OF:overflow flag,溢出标志,溢出为1 ZF:zero flag,结果为0标志,为零为1 P ...

  6. 【汇编】流程转移——标志寄存器(ZF—零标志、PF—奇偶标志、SF—符号标志、CF—进位标志、OF—溢出标志)、adc进位加法指令、sbb借位减法指令、cmp指令、DF标志、REP指令、std、cld

    11.1 标志寄存器 8086CPU中有14个寄存器,在历来文章中已经介绍过大部分,还有一个标志寄存器,在cpu中也担任着重要的位置. 在标志寄存器中是按位来起作用的,也就是说其中每一位的0和1都有着 ...

  7. 第二篇 溢出标志 CF与OF

    在汇编学习中,个人感觉CF与OF这两个溢出标志还是有点难理解的.笔者也还是一知半解,若有错误之处, 请指正! 一.学习CF与OF,要始终牢记一点.CF是无符号数溢出标志,OF是有符号数溢出标志. 通俗 ...

  8. 汇编程序设计与计算机体系结构软件工程师教程笔记:指令

    <汇编程序设计与计算机体系结构: 软件工程师教程>这本书是由Brain R.Hall和Kevin J.Slonka著,由爱飞翔译.中文版是2019年出版的.个人感觉这本书真不错,书中介绍了 ...

  9. 011-减法指令SUB及对标志寄存器PSW的影响

    l  汇编减法指令sub l   初识标志位 l   标志寄存器PSW l   ZF(零标志)     l  一.标志寄存器PSW 15 14 13 12 11 10 9 8 7 6 5 4 3 2 ...

最新文章

  1. 如何快速研究透一篇 Paper 并提出有价值的问题?
  2. CentOS/用FTP客户端软件连接到服务器
  3. vbs用mysql语句查询数据库_vbs脚本实现window环境下的mysql数据库的备份及删除早期备份...
  4. 资源大集中 浪潮I9000刀片为国家税务总局打造全能型平台
  5. 安卓mysql插入数据_【11-25求助】关于Android 的SQLite数据库插入数据报错问题
  6. es6笔记 day3---Promise
  7. CentOS 7.1.1503 varnish动静分离反代用户请求
  8. MyBatis多参数传递之混合方式——MyBatis学习笔记之十五
  9. php网站服务器工具,PHP网站服务器管理软件LuManager1.1.9发布
  10. OJ每日一练——鸡兔同笼问题
  11. 2020年06月16日_万金油_新浪博客
  12. 计算机函数average意思,average函数的功能是计算什么
  13. nginx的日志格式记录真实客户端IP
  14. ORB-SLAM2代码详解09: 闭环线程LoopClosing
  15. EFS】NV65633-LTE搜网能力的NV值、格式、配置
  16. MBps与Mbps区别
  17. 错误 CS0246 未能找到类型或命名空间名“XXXX”(是否缺少 using 指令或程序集引用?)
  18. 一汽大众android面试题,一汽大众面试题
  19. 一类形容词(形容词)的用法
  20. 电赛专题---一.概述【电赛简介 /信号类需要准备什么?/怎么才能打好电赛?】

热门文章

  1. 2015去哪儿网校园招聘笔试题:寻找字符串的差异
  2. 2010 我的求职经历(3)
  3. 阿里巴巴矢量图标库(网页)
  4. GUI图形化界面(后篇)
  5. 【kali-漏洞利用】(3.4)免杀Payload 生成工具(上):Veil安装、启动、Can‘t find the WINE profile问题
  6. 如何查看windows软件激活状态
  7. 磁盘IOPS和带宽(throughput)
  8. 指点迷津!十二星座程序猿个性,你属于哪个?
  9. SAS学习第9章:卡方检验之适合性检验与独立性检验
  10. 使用RTT代替UART,把你的JLink变成串口调试助手~