-
Notifications
You must be signed in to change notification settings - Fork 3
/
console.asm
71 lines (56 loc) · 2.2 KB
/
console.asm
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
; console.asm - An pure 32-bit and 64-bit win32 console program
; Made by Bastiaan van der Plaat (https://bplaat.nl/)
; 32-bit: nasm -f bin console.asm -o console-x86.exe && ./console-x86
; 64-bit: nasm -DWIN64 -f bin console.asm -o console-x64.exe && ./console-x64
%include "libwindows.inc"
header HEADER_CONSOLE
code_section
entrypoint
%define name_buffer_size 64
local console_out, POINTER_size, \
console_in, POINTER_size, \
name_buffer, name_buffer_size, \
name_bytes_read, DWORD_size, \
answer_buffer, 64
; Print question string
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov [console_out], _ax
invoke WriteConsoleA, [console_out], question, question_size, NULL, 0
; Read name answer string
invoke GetStdHandle, STD_INPUT_HANDLE
mov [console_in], _ax
invoke ReadConsoleA, [console_in], addr name_buffer, name_buffer_size, addr name_bytes_read, NULL
; Cut trailing CRLF enter characters
mov ecx, [name_bytes_read]
mov byte [name_buffer + _cx - 2], 0
; Check if name is empty if so replace with a question mark
mov al, [name_buffer]
if al, "==", 0
mov byte [name_buffer], "?"
mov byte [name_buffer + 1], 0
end_if
; Print formated answer string
cinvoke wsprintfA, addr answer_buffer, answer, addr name_buffer
invoke WriteConsoleA, [console_out], addr answer_buffer, _ax, NULL, 0
; Exit successfull
invoke ExitProcess, EXIT_SUCCESS
end_local
end_code_section
data_section
; String constants
question db "Hello, what is your name: "
question_size equ $ - question
answer db "Hello %s, it is nice to meet you!", 13, 10, 0
; Import table
import_table
library kernel_table, "KERNEL32.DLL", \
user_table, "USER32.DLL"
import kernel_table, \
ExitProcess, "ExitProcess", \
GetStdHandle, "GetStdHandle", \
ReadConsoleA, "ReadConsoleA", \
WriteConsoleA, "WriteConsoleA"
import user_table, \
wsprintfA, "wsprintfA"
end_import_table
end_data_section