《汇编语言》(王爽著)第11章 标志寄存器

来源:互联网 发布:淘宝电商代运营靠谱吗 编辑:程序博客网 时间:2024/05/17 23:16


NOTES:



P106

When executing arithmetic instructions, we often want to know something about the result. Is it negative, positive, or zero? Is it too large or too small to fit into the destination operand? Answers to such questions can help us detect calculation errors that might otherwise cause erratic program behavior. We use the values of CPU status flags to check the outcome of arithmetic operations. We also use status flag values to activate conditional branching instructions, the basic tools of
program logic. Here’s a quick overview of the status flags.


P39

The Status flags reflect the outcomes of arithmetic and logical operations performed by the CPU.They are the Overflow, Sign, Zero, Auxiliary Carry, Parity, and Carry flags.
Their abbreviations are shown immediately after their names:
• The Carry flag (CF) is set when the result of an unsigned arithmetic operation is too large to fit into the destination. 
• The Overflow flag (OF) is set when the result of a signed arithmetic operation is too large or too small to fit into the destination. 
• The Sign flag (SF) is set when the result of an arithmetic or logical operation generates a negative result. 
• The Zero flag (ZF) is set when the result of an arithmetic or logical operation generates a result of zero. 
• The Auxiliary Carry flag (AC) is set when an arithmetic operation causes a carry from bit 3 to bit 4 in an 8-bit operand.
• The Parity flag (PF) is set if the least-significant byte in the result contains an even number of 1 bits. Otherwise, PF is clear. In general, it is used for error checking when there is a possibility that data might be altered or corrupted.


DF , direction flag
ADC (add with carry) and SBB (subtract with borrow) 

;------------------------------------------------------------------------

检测点11.1

写出下面每条指令后,ZF、PF、SF等标志位的值。

sub al,al          ZF = 1        PF= 1         SF= 0  (  al = 0000 0000b)
mov al,1          ZF = 1        PF= 1         SF= 0  ( al = 0000 0001b,但因为mov是传送指令,对flags没有影响,故结果同上)
push ax           ZF = 1        PF= 1         SF= 0 同上,push是传送指令)

pop bx             ZF = 1        PF= 1        SF= 0   (POP对flags没有影响,所以flags没有变化)
add al,bl          ZF = 0        PF= 0        SF= 0      ( al = 0000 0010b)

add al,10        ZF = 0       PF= 1        SF=  0        al = 0000 1100b)
mul al              ZF = 0       PF= 1        SF=  0   (执行mul后,结果存在AX,所以此时相关的寄存器是AX,AX =0000 0000 1001 0000b)


检测点11.2

                            CF     OF     SF     ZF      PF    
sub al,al               0        0        0        1        1   
mov al,10H          0        0        0        1        1
add al, 90H         0        0        1        0        1    ;AL = 0A0H, 1010 0000B, 假设是有符号运算,10h + 90h = 16 + (-112) = - 96,   -96 在[-128, 127 ]范围内,故没有溢出,OF = 0 
mov al,80H          0        0        1        0        1   
add al,80H          1        1        0        1        1   ;假设是有符号运算,80h + 80h = -128 + (-128) = - 256, -256超出[-128, 127 ]范围,故OF = 1
mov al,0FCH      1        1        0        1        1    
add al,05H          1        0        0        0        0   ;AL = 01H,假设是有符号运算,0FCH + 05H = -4 + 5 = 1,故OF = 0
mov al,7DH         1        0        0        0        0
add al,0BH          0        1        1        0        1 ; 假设是有符号运算,7DH + 0BH =125 + 11 = 136,故OF = 1
 


检测点11.3
(1) 补全下面的程序,统计F000:0处32个字节中,大小在[32,128]的数据的个数。
mov ax,0f000h
mov ds,ax
mov bx,0
mov dx,0
mov cx,32
s:mov al,[bx]cmp al,32
jb s0
cmp al,120
ja s0
inc dx
s0:inc bx
loop s
(2) 补全下面的程序,统计F000:0处32个字节中,大小在(32,128)的数据的个数。
mov ax,0f000h
mov ds,ax
mov bx,0
mov dx,0
mov cx,32
s:mov al,[bx]
cmp al,32
jna s0
cmp al,120
jnb s0
inc dx
s0:inc bx
loop s


检测点11.4
下面的程序执行后:(ax)=?
mov ax,0
popf
mov ax,0fff0h
add ax,0010h        ;参照上面的“图11.1”,可确定的有,CF = 1, SF = 0 , ZF = 1, PF = 1, OF = 0
push ax
pushf
pop ax                     ; 把flag 的值赋给 ax
and al,11000101B ; 

and ah,00001000B; 使DF = IF = TF = 0

AX = 0000 0000 0100 0101B 或 0045H


实验11
assume cs:codesg
datasg segment
        db "Beginner's All-purpose Symbolic Instruction Code.",0
datasg ends
codesg segment
begin:
        mov ax,datasg
        mov ds,ax
        mov si,0
        call letterc
        mov ax,4c00h
        int 21h
letterc:
push si
push ax

next:
mov ch,0
mov cl,ds:[si] ; 检测字符是否为0
jcxz quit
mov al,ds:[si]
cmp al, ‘a’;-------------------- 也可以把‘a’换成97,即a的ascii码
jb s0
cmp al, ‘z’;-------------
ja s0
sub al, 32 ; ------32是同一字母的大小写如A、a,在ascii码中的差值,也可写成 sub al, 'a' - 'A'

s0:
mov ds:[si],al
inc si
jmp next

quit:
pop ax
pop si
ret

codesg ends
end begin

------------------------------------------------------------------

Reference:

KIP R. IRVINE, <Assembly Language for x86 Processors> Sixth Edition 

 CFOFSFZFPFsub al,al00011mov al,10H 00011add al,90H     mov al,80H     add al,80H      mov al,0FCH      add al,05H     mov al,7DH     add al,0BH     
0 0
原创粉丝点击