intel汇编语言程序设计 后面编程

5.1

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat,stdcall
.stack 4096

INCLUDE Irvine32.inc



.data
num DWORD 0FFFFFF9Ch
snum BYTE 21 DUP('A')
bnum DWORD 1,2,3,4,5,6,7
myArray WORD 20 DUP(?)
sizes = ($-myArray)
color WORD 0, 4*16, 8*16, 12*16

.code
main PROC
    mov ecx, LENGTHOF color
    mov esi, 0
    l1:
    mov ax, yellow
    add ax, color[esi]
    call SetTextColor
    add esi, TYPE color
    call DumpRegs
    loop l1
    INVOKE ExitProcess, 0
main ENDP
END main


5.2

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat,stdcall
.stack 4096

INCLUDE Irvine32.inc



.data
tmp DWORD ?
num = 45
dwData DWORD 1, 1, num DUP(?)
filename BYTE "myfile.txt", 0

.code
main PROC
    call createnum
    call writenum
    INVOKE ExitProcess, 0
main ENDP

createnum PROC
    pushad
    mov eax, 1
    mov ebx, 1
    mov ecx, num
    mov esi, 2*(TYPE dwData)
l1:
    mov tmp ,ebx
    add ebx, eax
    mov eax, tmp
    mov dwData[esi], ebx
    add esi, TYPE dwData
    loop L1
    popad
    ret
createnum ENDP


writenum PROC
    mov edx, OFFSET filename
    call CreateOutputFile
    mov edx, OFFSET dwData
    mov ecx, SIZEOF dwData
    call WriteToFile
    ret
writenum ENDP
END main

5.3

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
TAB = 9
.data
aName BYTE "Lincoln",0
nameSize = ($-aName) - 1
INTEGER_COUNT = 2
str1 BYTE "enter a :", 0
str2 BYTE "The sum is:", 0
array DWORD INTEGER_COUNT DUP(?)

.code
main PROC
call Clrscr
mov esi, OFFSET array
mov ecx, INTEGER_COUNT
call PromptForIntegers
call ArraySum
call DisplaySum
INVOKE ExitProcess, 0
main ENDP

PromptForIntegers PROC USES ecx edx esi
mov edx, OFFSET str1
L1:
call WriteString
call ReadInt
call Crlf
mov [esi], eax
add esi, TYPE DWORD
loop L1
ret
PromptForIntegers ENDP

ArraySum PROC USES esi ecx
mov eax, 0
L1:
add eax, [esi]
add esi, TYPE DWORD
loop L1
ret
ArraySum ENDP

DisplaySum PROC USES edx
mov edx, OFFSET str2
call WriteString
call WriteInt
call Crlf
ret
DisplaySum ENDP
END main


5.4

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
TAB = 9
.data
aName BYTE "Lincoln",0
nameSize = ($-aName) - 1
INTEGER_COUNT = 2
str1 BYTE "enter a :", 0
str2 BYTE "The sum is:", 0
array DWORD INTEGER_COUNT DUP(?)
num = 3

.code
main PROC
mov ecx, num
l0:
push ecx
call Clrscr
mov esi, OFFSET array
mov ecx, INTEGER_COUNT
call PromptForIntegers
call ArraySum
call DisplaySum
pop ecx
loop l0
INVOKE ExitProcess, 0

loop l0
main ENDP

PromptForIntegers PROC USES ecx edx esi
mov edx, OFFSET str1
L1:
call WriteString
call ReadInt
call Crlf
mov [esi], eax
add esi, TYPE DWORD
loop L1
ret
PromptForIntegers ENDP

ArraySum PROC USES esi ecx
mov eax, 0
L1:
add eax, [esi]
add esi, TYPE DWORD
loop L1
ret
ArraySum ENDP

DisplaySum PROC USES edx
mov edx, OFFSET str2
call WriteString
call WriteInt
call Crlf
ret
DisplaySum ENDP
END main


5.5

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc

.data
num = 50
TAB = 9
.code
main PROC
    mov ecx, num
l1:
    mov eax, 40
    call RandomRange
    sub eax, 20
    call WriteInt
    mov al, TAB
    call WriteChar
    loop l1
    INVOKE ExitProcess, 0
main ENDP

END main


5.6

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc

.data
index = 10
TAB = 9
totalnum = 20


.code
main PROC
mov ecx, totalnum
l0:
push ecx
mov al, TAB
call WriteChar
mov ecx, index
l1:
mov eax, 26
call RandomRange
add eax, 65
call WriteChar
loop l1

pop ecx
loop l0
INVOKE ExitProcess, 0
main ENDP

END main


5.7

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc

.data
index = 10
TAB = 9
totalnum = 100
nchar = 'A'


.code
main PROC
    mov ecx, totalnum
l0:
    call GetMaxXY
    movzx eax, dh
    call RandomRange
    mov dh, al
    movzx eax, dl
    call RandomRange
    mov dl, al
    call Gotoxy
    mov eax, nchar
    call WriteChar
    mov eax, 100
    call Delay
    loop l0

    INVOKE ExitProcess, 0
main ENDP

END main


5.8

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc

.data
num = 16
nchar DWORD 'A'

.code
main PROC
    mov ecx, num
    l0:
    push ecx
    mov eax, ecx
    sub eax, 1
    shl ax, 4
    
    mov ecx, num
    l1:
    add eax, ecx
    dec eax
    call SetTextColor
    mov eax, nchar
    call WriteChar
    loop l1

    pop ecx
    loop l0

    INVOKE ExitProcess, 0
main ENDP

END main


5.9

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
TAB = 9

.data
aName BYTE "Lincoln",0
nameSize = ($-aName) - 1
ARRAY_SIZE = 10
array DWORD ARRAY_SIZE DUP(?)
str1 BYTE "enter num :", 0
str2 BYTE "The sum is:", 0
str3 BYTE "How many intrgers wile be added?",0
str4 BYTE "The array cannot be larger than ",0


.code
main PROC
    call Clrscr
    mov edx, OFFSET str3
    call WriteString
    call ReadInt
    cmp eax, ARRAY_SIZE
    ja endsn
    mov esi, OFFSET array
    mov ecx, eax
    call PromptForIntegers
    call ArraySum
    call DisplaySum
    jmp endo
endsn:
    mov edx, OFFSET str4
    call WriteString
    mov eax, ARRAY_SIZE
    call WriteDec
endo:
    INVOKE ExitProcess, 0
main ENDP

PromptForIntegers PROC USES ecx edx esi
    mov edx, OFFSET str1
    L1:
    call WriteString
    call ReadInt
    call Crlf
    mov [esi], eax
    add esi, TYPE DWORD
    loop L1
    ret
PromptForIntegers ENDP

ArraySum PROC USES esi ecx
    mov eax, 0
    L1:
    add eax, [esi]
    add esi, TYPE DWORD
    loop L1
    ret
ArraySum ENDP

DisplaySum PROC USES edx
    mov edx, OFFSET str2
    call WriteString
    call WriteInt
    call Crlf
    ret
DisplaySum ENDP
END main


7.1

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
op1 QWORD 0000000000000002h, 0000000000000002h, 0000000000000002h, 9000000000000002h
op2 QWORD 0000000000000002h, 0000000000000002h, 0000000000000002h, 9000000000000002h
sum DWORD 9 DUP(0ffffffffh)


.code
Extended_Add PROC
    pushad
    clc
l1:
    mov eax, [esi]
    adc eax, [edi]
    pushfd
    mov [ebx], eax
    add esi, 4
    add edi, 4
    add ebx, 4
    popfd
    loop l1
    mov dword ptr[ebx],0
    adc dword ptr[ebx],0
    popad
    ret
Extended_Add ENDP

main PROC
    mov esi, OFFSET op1
    mov edi, OFFSET op2
    mov ebx, OFFSET sum
    mov ecx, 8
    call Extended_Add

    mov ecx, 9
    mov esi, 8
l2:    mov eax, sum[esi*TYPE DWORD]
    call WriteHex
    dec esi
    loop l2
    
    push 0
    call ExitProcess
main ENDP
END main


7.2

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
op1 QWORD 7000000000000002h, 0000000000000002h, 0000000000000002h, 9000000000000002h
op2 QWORD 8000000000000002h, 0000000000000002h, 0000000000000002h, 9000000000000002h
sum DWORD 9 DUP(0ffffffffh)


.code
Extended_sub PROC
    pushad
    clc
l1:
    mov eax, [esi]
    sbb eax, [edi]
    pushfd
    mov [ebx], eax
    add esi, 4
    add edi, 4
    add ebx, 4
    popfd
    loop l1
    mov dword ptr[ebx],0
    sbb dword ptr[ebx],0
    popad
    ret
Extended_sub ENDP

main PROC
    mov esi, OFFSET op1
    mov edi, OFFSET op2
    mov ebx, OFFSET sum
    mov ecx, 8
    call Extended_sub

    mov ecx, 9
    mov esi, 8
l2:    mov eax, sum[esi*TYPE DWORD]
    call WriteHex
    dec esi
    loop l2
    
    push 0
    call ExitProcess
main ENDP
END main

7.3

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
time WORD 0001001000000111b
astr BYTE ":",0


.code
ShowFileTime PROC
    push ax
    and ax, 11111b
    mov bx, 2
    mul bx
    mov bx, ax ;bx=second
    pop ax

    push ax
    shr ax, 5
    and ax, 11111b
    mov cx, ax
    pop ax

    push ax
    shr ax, 11
    and ax, 11111b
    mov dx, ax
    pop ax
    ret
ShowFileTime ENDP

main PROC
    mov eax, 0
    mov ax, time
    call ShowFileTime
    movzx eax, dx
    call WriteDec
    mov edx, OFFSET astr
    call WriteString
    movzx eax, cx
    call WriteDec
    mov edx, OFFSET astr
    call WriteString
    movzx eax, bx
    call WriteDec
    push 0
    call ExitProcess
main ENDP
END main


9.1

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
sourcestr BYTE "123456",0
targetstr BYTE 100 DUP(0)
max_length = 3   ;=10

.code


Str_copy_N PROC USES eax ecx esi edi,
                source:PTR BYTE,
                target:PTR BYTE,
                maxLength:DWORD
    INVOKE Str_length, source
    cmp eax, maxLength
    jbe l1
    mov eax, maxLength
l1:
    mov ecx, eax
    mov esi, source
    mov edi, target
    cld
    rep movsb
    ret
Str_copy_N ENDP

main PROC
    push max_length
    push OFFSET targetstr
    push OFFSET sourcestr
    call Str_copy_N
    mov edx, OFFSET targetstr
    call WriteString
    push 0
    call ExitProcess
main ENDP

END main

7.4

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
data1 DWORD 12h, 23h, 45h, 1111111h, 2222h
astr BYTE "  ",0
bstr BYTE 0dh, 0ah, 0

.code
shrd_ex PROC
    mov ecx, 4
l3:
    mov eax, [esi-4]
    shrd dword ptr[esi], eax, 16
    sub esi, 4
    loop l3

    ret
shrd_ex ENDP


main PROC
    mov ecx, LENGTHOF data1
    mov esi, 0
l1:
    mov eax, data1[esi*TYPE data1]
    call Writehex
    mov edx, OFFSET astr
    call WriteString
    inc esi
    loop l1
    mov ecx, 2
l3:
    push ecx
    mov esi, OFFSET data1 + 16
    call shrd_ex
    pop ecx
    loop l3

    mov edx, OFFSET bstr
    call WriteString

    mov ecx, LENGTHOF data1
    mov esi, 0
l2:
    mov eax, data1[esi*TYPE data1]
    call Writehex
    mov edx, OFFSET astr
    call WriteString
    inc esi
    loop l2

    push 0
    call ExitProcess
main ENDP
END main

7.5

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
.code
FastMultiply PROC
    mov edx, 32
    mov cl, 0
    mov edi, 0 ;save the sum
l1:  
    shr ebx, 1
    jnc l2
    push eax
    shl eax, cl
    add edi, eax
    pop eax
l2:
    inc cl
    dec edx
    cmp edx, 0
    jne l1
    
    mov eax, edi
    ret
FastMultiply ENDP


main PROC
    mov ebx, 23
    mov eax, 4
    call FastMultiply
    call WriteDec
    push 0
    call ExitProcess
main ENDP
END main

7.6

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
x SDWORD 25
y SDWORD -100


.code
GCD PROC
    cmp eax, 0
    jg l1
    neg eax
l1:
    cmp ebx, 0
    jg l2
    neg ebx
l2:
    
    cdq
    idiv ebx
    mov eax,ebx
    mov ebx, edx
    cmp ebx, 0
    jg l2

    ret
GCD ENDP


main PROC
    mov eax, x
    mov ebx, y
    call GCD
    call WriteDec
    push 0
    call ExitProcess
main ENDP
END main

7.7

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
astr BYTE "Is Prime", 0

.code
IsPrime PROC
    mov edi, eax
    mov ebx, 2
    mov edx, 0
    div ebx
    mov ecx, eax
l1:
    mov eax, edi
    mov edx, 0
    div ecx
    cmp edx,0
    jz l2
    cmp ecx,2
    loopnz l1
    and eax, 0
    jmp l3
l2: or eax, 1
l3:
    ret
IsPrime ENDP


main PROC
    mov eax, 7
    call IsPrime
    jnz l2
l1:
    mov edx, OFFSET astr
    call WriteString
    
l2: push 0
    call ExitProcess
main ENDP
END main


9.2

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
sourcestr BYTE "123456",0
targetstr BYTE "FTY", 10 DUP(0), 0

.code
Str_concat PROC,
        target:PTR BYTE,
        source:PTR BYTE
    pushad
    INVOKE Str_length, target
    mov esi, source
    mov edi, target
    add edi, eax
    INVOKE Str_length, source
    mov ecx, eax
    rep movsb
    popad
    ret
Str_concat ENDP

main PROC
    push OFFSET sourcestr
    push OFFSET targetstr
    call Str_concat
    mov edx, OFFSET targetstr
    call WriteString
    push 0
    call ExitProcess
main ENDP

END main


9.3

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
targetstr BYTE "abcxxxx1234567",0

.code
Str_remove PROC,
    pString:PTR BYTE,
    num:DWORD
    INVOKE Str_length, pString
    mov ecx, eax
    sub ecx, num
    mov edi, pString
    mov esi, edi
    add esi, num
    cld
    rep movsb
    mov byte ptr[edi], 0
    ret
Str_remove ENDP

main PROC
    push 4
    push OFFSET targetstr +3
    call Str_remove
    mov edx, OFFSET targetstr
    call WriteString
    push 0
    call ExitProcess
main ENDP

END main

9.4

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
target BYTE "112ABCDCDCD123456A", 0
source BYTE "ABC", 0

.code
Str_find PROC,
    sourcestr:ptr byte,
    targetstr:ptr byte

    INVOKE Str_length,  sourcestr
    mov edx, eax   ;获取源字符串长度
    INVOKE Str_length,  targetstr
    mov ecx, eax   ;获取目的字符串长度

    sub ecx, edx
    mov ebx, 0     ;目的字符串哨兵
l1:
    push ecx
    mov esi, sourcestr
    mov ecx, edx
    mov edi, targetstr
    add edi, ebx
    cld
    repe cmpsb
    jz l2  ;找到匹配字符串
    inc ebx
    pop ecx
    loop l1
    jmp l3 ;未找到匹配字符串
l2:
    pop ecx
    pushfd
    sub eax, ecx
    sub eax, edx
    popfd
l3:
    ret
Str_find ENDP

main PROC
    push OFFSET target
    push OFFSET source
    call Str_find
    push 0
    call ExitProcess
main ENDP

END main

9.5

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
target BYTE "Johnson,Calvin", 0

.code
Str_nextword PROC,
        targetstr:PTR BYTE,
        jchar:BYTE
    mov edi, targetstr
    mov al, jchar
    repne scasb
    je l1
    jmp l2
l1:
    mov eax, edi
    mov byte ptr[edi-1], 0
l2:
    ret
Str_nextword ENDP

main PROC
    push ','
    push OFFSET target
    call Str_nextword
    mov edx, OFFSET target
    call WriteString
    call DumpRegs
    push 0
    call ExitProcess
main ENDP
END main


9.6

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc


.data
target BYTE "ABCDEFAG",0
freqTable DWORD 256 DUP(0)

.code
Get_frequencies PROC,
        targetstr:PTR BYTE,
        freqTables:PTR DWORD
    mov ecx, targetstr
    mov edx, freqTables
    mov esi, 0
    mov edi, 0
l1:
    movzx eax, BYTE PTR[ecx + esi*TYPE BYTE]
    cmp eax, 0
    je l2
    inc dword ptr[edx + eax*TYPE DWORD]
    inc esi
    inc edi
    jmp l1
l2:
    ret
Get_frequencies ENDP

main PROC
    push OFFSET freqTable
    push OFFSET target
    call Get_frequencies
    push 0
    call ExitProcess
main ENDP
END main

9.7

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
num = 200
.data?
array BYTE num DUP(?)
half DWORD ?
.data
strt BYTE "    ", 0

.code
main PROC
    mov ecx,LENGTHOF array
    mov al, 0
    mov edi, OFFSET array
    cld
    rep stosb

    mov edx, 0
    mov eax, num
    mov ebx, 2
    div ebx
    mov half, eax

    mov ebx, OFFSET array  ;start addr of array
    mov edi, ebx
    add edi, 2*TYPE BYTE
l1:
    mov al, 1
    mov ecx, num
    cld
    repe scasb
    mov ecx, edi
    sub ecx, ebx
    dec ecx
    cmp ecx, half
    ja endw
    mov eax, 2
l3:
    push eax
    mul ecx
    cmp eax, num
    jae l2
    mov array[eax*TYPE BYTE], 1
    pop eax
    inc eax
    jmp l3
l2:
    jmp l1
endw:
    mov ecx, LENGTHOF array
    mov esi, 0
l4:
    cmp array[esi], 0
    jne l5
    mov eax, esi
    call WriteDec
    mov edx, OFFSET strt
    call WriteString
l5:
    inc esi
    loop l4
    push 0
    call ExitProcess
main ENDP
END main

9.8

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
.data
array DWORD 2, 1, 3, 4

.code
BubbleSort PROC USES eax ecx esi,
        pArray:PTR DWORD,
        Count:DWORD
    mov ecx, Count
    dec ecx
    mov edx, 0
l1:
    push ecx
    mov esi, pArray
    mov edx, 0  ;检查位
l2:
    mov eax, [esi]
    cmp [esi+4], eax
    jg l3
    mov edx, 1
    xchg eax, [esi+4]
    mov [esi], eax
l3: add esi, 4
    loop l2
    pop ecx
    cmp edx, 0
    je l4
    loop l1
l4:    ret
BubbleSort ENDP

main PROC
    mov edi, OFFSET array
    mov ecx, LENGTHOF array
    push ecx
    push edi
    call BubbleSort
    push 0
    call ExitProcess
main ENDP
END main


9.9

TITLE Add and Subtract

; This program adds and subtracts 32-bit integers.
; Last update: 06/01/2006

.386
.model flat, stdcall
.stack 4096

INCLUDE Irvine32.inc
.data
array DWORD 1,2,4,5,9,12,18,20,100,102

.code
BinarySearch PROC uses ebx edx esi edi,
            pArray:PTR DWORD,
            Count:DWORD,
            searchVal:DWORD

    mov eax, 0; eax=first
    mov ebx, Count
    dec ebx   ;ebx=last
    mov edi, searchVal
    mov edx, pArray
l1:
    cmp eax, ebx
    jg l5
    push eax
    push ebx
    add eax, ebx
    shr eax, 1
    mov esi, eax
    shl esi, 2
    mov ebx, [edx+esi]
    cmp ebx, edi
    jge l2
    pop ebx
    pop esi
    inc eax
    jmp l4
l2:
    cmp ebx, edi
    jle l3
    mov ebx, eax
    dec ebx
    pop esi
    pop eax
    jmp l4
l3:
    pop esi
    pop esi
    jmp l9
l4:
    jmp l1
l5:
    mov eax, -1
l9:    ret
BinarySearch ENDP


main PROC
    mov eax, 103
    push eax
    push LENGTHOF array
    push OFFSET array
    call BinarySearch
    call WriteDec
    push 0
    call ExitProcess
main ENDP
END main

9.10

.model flat, stdcall

INCLUDE Irvine32.inc

.data
astr BYTE "AEIOU"
bstr BYTE "BCDFGHJKLMNPQRSTVWXYZ"
cstr BYTE  0dh,0ah,0
dstr BYTE 0dh,0ah,0dh,0ah,0dh,0ah,0dh,0ah,0

.code

main PROC
    mov ecx, 3
l6:
    push ecx
    mov ecx, 4
l5:
    push ecx
    mov ecx, 4
l4:
    mov eax, 2
    call RandomRange
    cmp eax, 0
    je l1
    mov eax, LENGTHOF astr
    call RandomRange
    mov bl, astr[eax*TYPE BYTE]
    jmp l2
l1:
    mov eax, LENGTHOF bstr
    call RandomRange
    mov bl, bstr[eax*TYPE BYTE]
l2:
    mov al, bl
    call WriteChar
    loop l4
    mov edx, OFFSET cstr
    call WriteString
    pop ecx
    loop l5
    mov edx, OFFSET dstr
    call WriteString
    pop ecx
    loop l6
    exit
main ENDP
END main

9.11

.model flat, stdcall

INCLUDE Irvine32.inc

.data
astr BYTE "AEIOU"
arrstr BYTE  'P','O','A','Z'
       BYTE  'A','E','A','U'
       BYTE  'G','K','A','E'
       BYTE  'I','B','G','D'
bstr BYTE 0dh,0ah,0
    

.code

dect PROC
    push ebp
    mov ebp, esp
    push esi
    push ecx
    push ebx
    push edi
    
    mov esi, [ebp+8]   ;addr of array
    mov ecx, [ebp+12]  ;count
    mov ebx, 0
l1:
    push ecx
    cld
    lodsb
    mov ecx, LENGTHOF astr
    mov edi, OFFSET astr
    repne scasb
    jne l2
    inc ebx
l2:
    pop ecx
    loop l1

    mov eax, ebx
    pop edi
    pop ebx
    pop ecx
    pop esi
    leave
    ret 8
dect ENDP

displaychar PROC
    push ebp
    mov ebp, esp
    push esi
    push ecx
    push eax
    mov esi, [ebp+8]
    mov ecx, [ebp+12]
    ld:
    lodsb
    call WriteChar
    loop ld
    pop eax
    pop ecx
    pop esi
    leave
    ret 8
displaychar ENDP

main PROC
  mov esi, 0
  mov ecx, 4
lr:
  mov eax, 4
  push eax
  mov ebx, OFFSET arrstr
  add ebx, esi
  push ebx
  call dect
  cmp eax, 2
  jne ltb
  push 4
  push ebx
  call displaychar
  mov edx, OFFSET bstr
  call WriteString
 ltb:
  add esi,4
  loop lr

  push 0
  call ExitProcess
main ENDP
END main

9.12

.model flat, stdcall

INCLUDE Irvine32.inc

.data
arrstr WORD  2,34,3,56
rowsize = $ - arrstr
       WORD  2,2,2,2
       WORD  4,5,1,0
       WORD  0,1,3,5
    

.code
calc_row_sum PROC
    push ebp
    mov ebp, esp
    mov esi, [ebp+8];数组偏移
    mov eax, [ebp+12];行的大小
    mov ebx, [ebp+16];数组类型
    push eax
    mov edx, 0
    div ebx
    mov ecx, eax ;ecx为行的元素个数
    pop eax
    mov edx, [ebp+20];行的索引
    mul edx
    mov edi, eax
    add edi, esi ;edi为行元素首地址
    mov esi, 0
l1:
    cmp ebx, 1
    jne l2
    movzx eax, byte ptr[edi+esi*1]
    jmp l4
l2:    cmp ebx, 2
    jne l3
    movzx eax, word ptr[edi+esi*2]
    jmp l4
l3:    mov eax, dword ptr[edi+esi*4]
l4:
    add edx, eax
    inc esi
    loop l1
    mov eax, edx
    leave
    ret 16
calc_row_sum ENDP

main PROC
  mov eax, 2 ;行索引
  push eax
  mov eax, TYPE arrstr ;数组的类型
  push eax
  mov eax, rowsize ;行的大小
  push eax
  push OFFSET arrstr ;数组偏移
  call calc_row_sum
  call WriteDec
  push 0
  call ExitProcess
main ENDP
END main



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值