I am still learning to use MASM. I am using the 64 bit VS version. I have designed a couple of functions to read from and print to console
BTW I am calling the MASM functions with the proper extern "C" statements, from a C++ main for now.
I was wondering if someone could help with the output.
(FORGIVE ME, I WANTED TO INSERT AN IMAGE, BUT CAN'T FIGURE OUT HOW)
COMMANDLINE OUTPUT>
Hello, Tom, from VS2017 MASM64 - prints first
Hi there, VS2017 MASM64, thank you! - I enter some input
Hi there, VS2017 MASM k you! - echos input to screen
My issue is that if I wrote less than the number of bytes where the part where it skips, it prints normally, it does this when it is more than 20 or so characters. I have allocated a buffersize of 64 bytes. so it should be fine.
I am using WinAPI, ReadFile and WriteFile, here is my code:
; *************************************************************************
; MASM32 proto types for Win32 functions and structures
; *************************************************************************
;include c:\masm32\include\kernel32.inc
;include c:\masm32\include\masm32.inc
; *************************************************************************
; MASM32 object libraries
; *************************************************************************
includelib c:\masm32\lib\masm32.lib
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\user32.lib
includelib Kernel32.lib
extern MessageBoxA: proc
extern CreateThread: proc
extern WriteFile: proc
extern ReadFile: proc
extern GetStdHandle: proc
MAX_SIZE equ 10
STR_LEN equ sizeof hello
BUF_LEN equ 40h
STD_INPUT_HANDLE equ -10
STD_OUTPUT_HANDLE equ -11
INVALID_HANDLE_VALUE equ -1
.data
mybyte1 db 4
mybyte2 db 1
mutex dword 0 ; mutex
BytesWritten dword 0
BytesToRead dword 0
hello db "Hello, Tom, from VS2017 MASM64", 13, 10
shit db "SHITHEAD!", 0
name_prompt db "Please type your name: %c", 10, 0
out_msg db "Your name in capitals is ", 0
.data?
array db ?
mynum qword ?
num dword ?
shifter qword ?
hFile qword ?
my_name db ?
.code
PrintString MACRO string, size, readLen
; https://blogs.msdn.microsoft.com/oldnewthing/20160623-00/?p=93735
sub rsp, 40 ; Shadow space (4 * 8) & 1 parameter (8 bytes)
; https://docs.microsoft.com/en-us/cpp/build/stack-allocation
and spl, -16 ; Align to 16
; https://msdn.microsoft.com/library/windows/desktop/ms683231.aspx
mov ecx, STD_OUTPUT_HANDLE ; DWORD nStdHandle = STD_OUTPUT_HANDLE
call GetStdHandle ; Call WinApi
mov hFile, rax ; Save returned handle
mov bl, 1 ; Set ZF = 1
; https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile
; BOOL WINAPI WriteFile(
mov rcx, hFile ; HANDLE hFile (here: Stdout)
lea rdx, string ; LPCVOID lpBuffer
lea r9, readLen ; LPDWORD lpNumberOfBytesWritten
mov r8d, size ; DWORD nNumberOfBytesToWrite
mov qword ptr [rsp+32], 0 ; LPOVERLAPPED lpOverlapped = NULL
call WriteFile ; Call WinAPI
; );
test bl, al ; test ZF for errors
mov readLen, 0 ; Zero out the variable for reuse
; https://msdn.microsoft.com/library/windows/desktop/ms682658.aspx
xor rcx, rcx ; Set RCX / Close HANDLE
add rsp, 40
ret
endm
getCmdLineInput proc
; https://blogs.msdn.microsoft.com/oldnewthing/20160623-00/?p=93735
sub rsp, 40 ; Shadow space (4 * 8) & 1 parameter (8 bytes)
; https://docs.microsoft.com/en-us/cpp/build/stack-allocation
and spl, -16 ; Align to 16
mov ecx, STD_INPUT_HANDLE ; DWORD nStdHandle = STD_INPUT_HANDLE
call GetStdHandle ; Call WinApi
mov hFile, rax ; Save returned handle
mov bl, 1 ; Set ZF = 1
; https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-readfile
; BOOL ReadFile(
mov rcx, hFile ; HANDLE hFile,
lea rdx, array ; LPVOID lpBuffer,
mov r8d, BUF_LEN ; DWORD nNumberOfBytesToRead,
lea r9, BytesWritten ; LPDWORD lpNumberOfBytesRead,
mov qword ptr [rsp+32], 0 ; LPOVERLAPPED lpOverlapped
call ReadFile ; Call WinAPI
; );
test bl, al ; test ZF for errors
xor rcx, rcx ; Set RCX / Close HANDLE
add rsp, 40
PrintString array, BytesWritten, BytesToRead
ret
getCmdLineInput endp
I have a question about macros... do you typically put in the full code with the ret?
I haven't finished error checking, as for testing for errors, like invalidHandle (-1)... is that correct using the test, then a jz or jne?
Thanks
PS: I realized after I wrote this, that it may be in the wrong forum... Admin, please feel free to put it where it goes.