|
查看: 1830|回复: 3
|
Assembly language intel based 关于 Carry Flag
[复制链接]
|
|
|
各位大大,据我所知道的Carry Flag 应该是在unsigned 的的type是当通过加减而超过本身 type 的容量那时才会出现”1“。8bit 的 register 最大的数字应该是 255 但是我从255 加到256 然后再减1 变回255 那时为什么CF 还是会一呢?而且SF 也是有1.应该是正数的,不同为什么SF会出现1.
先谢谢各位。一下是我的程式跟答案
TITLE Chapter 4 Exercise 1 (ch04_01.asm)
Comment !
Description: Write a program that uses addition and subtraction to set and
clear the Carry flag. After each instruction, insert the call DumpRegs
statement to display the registers and flags. Using comments, explain how
(and why) the Carry flag was affected by each instruction.
Author: Tan Deik Hoong
!
INCLUDE Irvine32.inc
.code
main PROC
;adding 1 to 255 rolls AL over to zero:
mov al,255
add al,1 ; AL=0, CF=1 (unsigned overflow)
call DumpRegs
;subtracting larger number from smaller:
sub al,1 ; AL=255, CF=1
call DumpRegs
;subtracting 1 from 255
sub al,1 ; AL=254, CF=0
call DumpRegs
exit
main ENDP
END main
答案:
EAX=75BB1100 EBX=7FFDA000 ECX=00000000 EDX=00401000
ESI=00000000 EDI=00000000 EBP=0012FF94 ESP=0012FF8C
EIP=00401009 EFL=00000257 CF=1 SF=0 ZF=1 OF=0 AF=1 PF=1
EAX=75BB11FF EBX=7FFDA000 ECX=00000000 EDX=00401000
ESI=00000000 EDI=00000000 EBP=0012FF94 ESP=0012FF8C
EIP=00401010 EFL=00000297 CF=1 SF=1 ZF=0 OF=0 AF=1 PF=1
EAX=75BB11FE EBX=7FFDA000 ECX=00000000 EDX=00401000
ESI=00000000 EDI=00000000 EBP=0012FF94 ESP=0012FF8C
EIP=00401017 EFL=00000282 CF=0 SF=1 ZF=0 OF=0 AF=0 PF=0 |
|
|
|
|
|
|
|
|
|
|
发表于 17-11-2010 02:36 PM
|
显示全部楼层
8bit 的 register 最大的数字应该是 255 但是我从255 加到256 然后再减1 变回255
invisible 发表于 12-11-2010 12:43 PM 
最大是255,最小是0,没有256,因为256已经包括0..... |
|
|
|
|
|
|
|
|
|
|
发表于 17-11-2010 05:18 PM
|
显示全部楼层
本帖最后由 bookm 于 17-11-2010 05:24 PM 编辑
回复 1# invisible
对不起,没仔细看好。
这样来理解:
CF=unsigned overflow
SF=negative sign
CF=1 当 AL>255 或 AL<0
SF=1 当 AL<0 或 AL--
所以,
1. AL=255 再 INC AL 自然是CF=1,SF=0
2. AL=0 再 DEC AL 自然是 SF=1,CF=1
3. AL=255 再 DEC AL 的话,CF应该是0,SF会是1 因为是减的算术*。
*SF indicates whether a subtraction or addition has taken place, whereas the NF indicates whether the last operation result is positive or negative.
CF=Indicates that the result of an operation produced an answer greater than the number of available bits.
希望可以帮到彼此。(不肯定对不对) |
|
|
|
|
|
|
|
|
|
|
发表于 20-11-2010 02:07 AM
|
显示全部楼层
本帖最后由 chrizyuen2 于 20-11-2010 02:12 AM 编辑
CF 是进位. SF=0 前进, SF=1退位。
16bitData=00FF + 01 ,溢满(overflow) /装不下了。所以进位 CF=1, 16bitData=0100
16bitData=0100 -1 ,不够,所以借数退位 CF=1 ,SF=1 16bitData=00FF
8bitData=FF + 01 ,溢满(overflow) /装不下了。所以进位 CF=1, 16bitData=00
8bitData=00 -1 ,不够,所以借数退位 CF=1 ,SF=1 16bitData=00, Data=FF
这时SF 可以当成负数 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|