天天看点

Intel汇编语言程序设计第四章答案

4.1

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.code
main proc
    ; CF = 1
    mov al, 0FFh
    add al, 1
    call DumpRegs
    ; CF = 0
    sub al, 0
    call DumpRegs

    exit
main endp
end main
           

4.2

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.code
main proc
    ; CF == 0
    xor eax, eax 
    mov al, 0FFh
    call DumpRegs
    ; CF == 1
    add al, 1
    call DumpRegs
    ; CF == 0
    xor eax, eax 
    mov al, 0FFh
    call DumpRegs
    ; CF == 0
    inc al
    call DumpRegs

    exit
main endp
end main
           

4.3

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.code
main proc
    ; 让al溢出变成0, 此时ZF=1, CF=1
    mov al, 0FFh
    add al, 1
    call DumpRegs
    ; 给al加1, 不溢出不为0所以ZF=0, CF=0
    add al, 1
    call DumpRegs

    exit
main endp
end main
           

4.4

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.code
main proc
    ; 加法设置OF
    mov al, +127
    add al, 1
    call DumpRegs
    ; 减法清除OF
    sub al, 0
    call DumpRegs

    exit
main endp
end main
           

4.5

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
Uarray WORD 1000h, 2000h, 3000h, 4000h
Sarray SWORD -1, -2, -3, -4

.code
main proc
    movzx eax, WORD PTR [Uarray]
    movzx ebx, WORD PTR [Uarray + 2]
    movzx ecx, WORD PTR [Uarray + 4]
    movzx edx, WORD PTR [Uarray + 6]
    call DumpRegs
    movsx eax, WORD PTR [Sarray]
    movsx ebx, WORD PTR [Sarray + 2]
    movsx ecx, WORD PTR [Sarray + 4]
    movsx edx, WORD PTR [Sarray + 6]
    call DumpRegs

    exit
main endp
end main
           

4.6

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
f0 DWORD 0

.code
main proc
    mov ecx, 12 
    mov eax, 1
L1:
    call DumpRegs 
    mov ebx, f0 
    mov f0, eax
    add eax, ebx
    loop L1 

    exit
main endp
end main
           

4.7

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
val1 SWORD 8 
val2 SWORD -15
val3 SWORD 20

.code
main proc
    xor eax, eax ; 0x0
    neg val2     ; 0x0
    mov ax, val2 ; 0xF
    add ax, 7    ; 0x16
    sub ax, val3 ; 0x2
    add ax, val1 ; 0xA
    call DumpRegs

    exit
main endp
end main
           

4.8

include Irvine32.inc
includelib Irvine32.lib
includelib Kernel32.Lib
includelib User32.lib

.data 
; 源字符
source BYTE "This is the source string", 0
; 源字符串长度
source_length DWORD SIZEOF source - 1
; 目的字符串
target BYTE SIZEOF source DUP('#')
; 目的字符串长度
target_length DWORD SIZEOF target - 1

.code
main proc
    ; 先把空字符赋值过去
    mov ecx, target_length
    mov BYTE PTR [target + ecx], 0
    
    xor eax, eax 
    xor esi, esi 
    ; 获取dst缓冲区大小(除了空字符)
    mov edi, target_length
    dec edi
L1:
    ; 获取src的一个字符
    mov al, [source + esi]
    ; 把src的字符赋值到dst末尾
    mov [target + edi], al
    inc esi 
    dec edi
    loop L1 
    
    ; 显示字符串
    mov esi, OFFSET target 
    mov ebx, 1
    mov ecx, SIZEOF target 
    call DumpMem
    exit
main endp
end main
           

(完)

继续阅读