-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathListing_9_1.txt
87 lines (69 loc) · 3.28 KB
/
Listing_9_1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
includelib kernel32.lib ; Windows kernel interface
GetStdHandle proto ; Function to retrieve I/O handles
WriteConsoleA proto ; Function that writes to command window
ReadConsoleA proto ; Function that reads from command window
Console equ -11 ; Device code for console text output.
Keyboard equ -10 ; Device code for console text input.
MaxBuf equ 40 ; Maximum input buffer size
ExitProcess proto
; Macro "txtOut msg,nchar" displays a character string in command window.
; msg: Address of ASCII message for command window.
; nchar: Address of length of message in bytes
txtOut macro msg,nchar ; Message location and length
mov RCX,stdout ; Handle to standard output device
lea RDX,msg ; Pointer to message to display
mov R8,nchar ; Number of characters to display
lea R9,nbwr ; Number of bytes actually written.
call WriteConsoleA ; Write text string to command window.
endm
.code
; Main program that reads text message from user through command
; window keyin and displays it in same command window.
; 1. Multiple lines are input until only "Enter" key pushed.
; 2. Each line will be output twice: as input and a copy.
main proc
sub RSP,40 ; Reserve "shadow space" on stack.
; Obtain "handles" for console I/O streams
mov RCX,Console ; Console standard output handle
call GetStdHandle ; Returns handle in register RAX
mov stdout,RAX ; Save handle of console display.
mov RCX,Keyboard ; Console standard input handle
call GetStdHandle ; Returns handle in register RAX
mov stdin,RAX ; Save handle for keyboard input.
; Display the prompt message.
nxtlin: txtOut pmsg,plen ; Write text string to command box.
; Read input line from user keyboard.
mov RCX,stdin ; Handle to standard input device
mov R8,MaxBuf ; Maximum length to receive
lea RDX,keymsg ; Memory address to receive input
lea R9,nbrd ; Number of bytes actually read.
call ReadConsoleA ; Read text string from command box.
txtOut keymsg,nbrd ; Write text back to command box.
; Copy message to a second buffer and display it, too.
lea RSI,keymsg ; Points to input buffer
lea RDI,dismsg ; Points to display buffer
mov RCX,MAXBUF ; Size of buffer in bytess
cpylp: lodsb ; Load next byte and increment RSI.
stosb ; Store byte from AL and increment RDI.
loop cpylp ; Continue until all copied.
txtOut dismsg,nbrd ; Write text back to command box.
; Go get another line, but exit if only "Enter" key was input.
mov R8,nbrd ; Length (bytes) of input message
cmp R8,2 ; Test if only CR and LF characters.
jg nxtlin ; Loop back around to get another input.
add RSP,40 ; Replace "shadow space" on stack
mov RCX,0 ; Set exit status code to zero.
call ExitProcess ; Return control to Windows.
main endp
.data
pmsg byte "Please enter text message: "
align 16 ; Set memory location on 16 byte boundary.
cvt byte 16 DUP (20h) ; Pattern to convert letter case.
plen qword lengthof pmsg ; Number of bytes in prompt message.
stdout qword ? ; Handle to standard output device
nbwr qword ? ; Number of bytes actually written
stdin qword ? ; Handle to standard input device
nbrd qword ? ; Number of bytes actually read
keymsg qword MaxBuf DUP (?) ; Memory buffer for keyboard input
dismsg qword MaxBuf DUP (?) ; Memory buffer for display
end