The fifth chapter process

 5.4 stack operation

5.4.2 POP and PUSH instruction

The 32-bit instruction PUSHFD content EFLAGS register onto the stack, the top location POPFD put the contents of the pop instructions to the EFLAGS register:

PUSHAD accordance with instructions EAX, ECX, EDX, EBX, ESP ( PUSHAD value before execution), EBP, ESI and EDI order, all 32-bit general registers onto the stack.

POPAD same instructions in reverse order to eject the stack register. Similarly, PUSHA sequential instruction (AX, CX, DX, BX , SP, BP, SI and DI) 16-bit general registers onto the stack.

POPA instructions in reverse order to eject the same register stack. In 16-bit mode, only PUSHA and POPA instructions.

 

 

5.8 Programming Exercises

1. Draw colored text

TITLE test project

INCLUDE Irvine32.inc

.data
buffer BYTE 'Hk_Mayfly',13,10,0
color DWORD 1,2,3,4,6

.code
main PROC
mov esi,OFFSET color
mov edx,OFFSET buffer
mov ecx,4
S:
mov eax,[esi]
shl eax,4
add eax,[esi+TYPE color]
call SetTextColor
call WriteString
add esi,TYPE color
loop S

call WaitMsg
exit
main ENDP
END main

 

2. Fibonacci file

TITLE the Test Project 

the INCLUDE Irvine32. INC. 

Buf_size = 47 

.data 
filename BYTE ' Fib.txt ' , 0 
FileHandle DWORD? ; File handle 
the Num DWORD buf_size dup ( 0 ) 

.code 
main PROC 
; handle for the file to build relationships 
mov edx, OFFSET filename
 Call CreateOutputFile
 MOV FILEHANDLE, EAX 

MOV ESI, 0 
MOV [the Num], . 1 
MOV [the Num the Num the TYPE +], . 1 
MOV ECX, 45 

;Third calculation cycle to 47 Fibonacci numbers 
S: 
MOV EAX, [ESI + the Num]
 the Add EAX, [ESI + the Num the Num + the TYPE]
 the Add ESI, the Num the TYPE
 MOV [the Num the Num the TYPE + + ESI], EAX 
Loop S 

; the data is written to a file Num 
MOV EAX, FILEHANDLE
 MOV EDX, the OFFSET Num
 MOV ECX, SIZE Num
 Call the WriteToFile 

; the display data to the terminal in Num 
MOV ESI, the OFFSET Num
 MOV ECX, lengthof Num
 MOV EBX, the TYPE Num
 Call DumpMem 

Call WaitMsg 
Exit 
main ENDP 
the END main

 

3. The simple addition (1)

TITLE Test Project 

the INCLUDE Irvine32. Inc is 

Strl EQU < ' Please TWO INPUT NUM: ' , 13 is , 10 , 0 > 

.data 
Str2 in BYTE Strl 

.code 
main the PROC 
; clear screen 
Call clrscr
 ; Position the cursor 
MOV DH, 12 is 
MOV DL, 39 
Call gotoxy
 ; output prompt string 
MOV EDX, the OFFSET Str2 in
 Call WriteString
 ; reading a first number, to save EBX 
Call ReadInt
 MOV EBX, EAX
Call ReadInt
 ; reads the second number, and added to the first number, and to obtain, in the screen display 
the Add EAX, EBX
 Call writeInt 

Call WaitMsg 
Exit 
main ENDP 
the END main

 

 

4. simple addition (2)

TITLE Test Project 

. Irvine32 the INCLUDE inc is 

Strl EQU < ' Please TWO INPUT NUM: ' , 13 is , 10 , 0 > 

.data 
Str2 in BYTE Strl 

.code 
main the PROC 
    MOV ECX, . 3 
S: 
    ; clear screen 
    Call clrscr
     ; Position the cursor 
    MOV DH, 12 is 
    MOV DL, 39 
    Call gotoxy
     ; output prompt string 
    MOV EDX, the OFFSET Str2 in
     Call WriteString
     ; reading a first number, to save ebx
    Call ReadInt
     MOV EBX, EAX
     Call ReadInt
     ; reads the second number, and added to the first number, and to obtain, in the screen display 
    the Add EAX, EBX
     Call writeInt
     Call WaitMsg 
    Loop S 

Call WaitMsg 
Exit 
main ENDP 
the END main

 

 

5. random integer

TITLE test project 

INCLUDE Irvine32. Add 

.data 
randVal DWORD 50 dup (?) 

.code 
main PROC 
    mov ECX, 50 
    mov esi, 0 
S: 
    mov EAX, 41 
    call Random Range
     mov [esi + randVal], EAX
     sub [randVal + esi], 20 
    add esi, TYPE randVal 
    walk S 

    mov esi, OFFSET randVal
     mov ECX, LENGTHOF randVal
     mov EBX, TYPE randVal
     call DumpMem 

call  WaitMsg
exit
main ENDP
END main

 

6. random string

TITLE test project 

INCLUDE Irvine32. Add 

.data 
randVal BYTE 13 dup ( 0 ) 

.code 
main PROC 
    mov ECX, 20 
S: 
    push ECX
     mov esi, 0 
    mov ECX, 10 
    S2: 
        mov EAX, 26 
        call Random Range
         mov [esi + randVal], all
         add [randVal + esi], 65 
        add esi, 1 
    walk S2 
    mov [esi + randVal] 10 
    add esi,1 
    mov [randVal + esi], 13 
    add esi, 1 
    mov [randVal + esi], 0 
    mov EDX, OFFSET randVal
     call WriteString
     pop ECX 
walk S 
    mov esi, OFFSET randVal
     mov ECX, LENGTHOF randVal
     mov EBX, 1 
    call DumpMem 

call WaitMsg 
exit 
main ENDP 
END main

 

 

7. random screen position

TITLE Test Project 

. Irvine32 the INCLUDE inc is 

.code 
main the PROC 
MOV ECX, 100 
S: 
    MOV eax, 0 
    Call GetMaxXY ; return value dx, the DH line memory, memory columns DL 
    MOV Al, DL
     Call RandomRange ; imported from the eax, the return value is stored in eax 
    MOV DL, Al 

    MOV Al, DH
     Call RandomRange
     MOV DH, Al 

    Call gotoxy ; imported from the dx 
    MOV Al, ' A ' 
    Call WriteChar 

    MOV eax,100
    call Delay
    loop S
    

call WaitMsg
exit
main ENDP
END main

 

8. Color Matrix

TITLE test project

INCLUDE Irvine32.inc

.data
Str1 BYTE 'CTF{It_is_easy}',13,10,0

.code
main PROC
    mov eax,0
    mov esi,0
    mov ecx,15
S:
    push ecx
    mov ecx,15
S1:
    shl esi,4
    add eax,esi
    call setTextColor
    mov edx,OFFSET Str1
    call WriteString
    inc esi
    loop S1

    pop ecx
    inc eax
    loop S

call WaitMsg
exit
main ENDP
END main

 

9. summation program

TITLE test project

INCLUDE Irvine32.inc

ARRAY_SIZE = 20

.data
Info BYTE 'How many integers will be added?',13,10,0
Info1 BYTE "The array cannot be large than",0
LineFeed BYTE 13,10,0

.code
main PROC
;输入数组大小
mov edx,OFFSET Info
call WriteString
call ReadInt
;比较是否越界
cmp eax,ARRAY_SIZE
jbe success
;越界输出
mov edx,OFFSET Info1
call WriteString
mov eax,ARRAY_SIZE
call WriteInt
mov edx,OFFSET LineFeed
call WriteString

success:
call WaitMsg
exit
main ENDP
END main

Guess you like

Origin www.cnblogs.com/Mayfly-nymph/p/11537791.html