From 24aabcf0a06b541fb41375bfe0ec6ed586f4c34b Mon Sep 17 00:00:00 2001 From: ergz Date: Wed, 23 Nov 2022 00:26:35 -0800 Subject: [PATCH] adds user input program --- src/1_8.asm | 57 ++++++++++++++++++++++----------------------------- src/build.bat | 2 +- src/c.cpp | 1 - 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/1_8.asm b/src/1_8.asm index df3f986..170cdb6 100644 --- a/src/1_8.asm +++ b/src/1_8.asm @@ -1,21 +1,16 @@ -; assembly that shows how to return values to a -; a C function - option casemap:none ; constants -nl = 10 -maxlen = 256 +nl = 10 ; newline +maxlen = 256 ; max len of the string .data -title_str byte "Listing 1-8", 0 -prompt byte "Enter a string: ", 0 -fmt_string byte "User entered: '%s'", nl, 0 - -; make `maxlen` copies/duplicates of size byte, each uinitialized -input byte maxlen dup (?) +title_string byte 'Listing 1_8', 0 +prompt_string byte 'Enter a string: ', 0 +format_string byte 'User entered: "%s" ', nl, 0 +input byte maxlen dup (?) ; make maxlen copies of byte, each uninitialized .code @@ -23,39 +18,37 @@ input byte maxlen dup (?) externdef readline:proc public get_title - -; procedure loads the address of the title_str into the RAX -; register, this is the register where C expects to find -; the return of this proc. (WINDOWS ABI) get_title proc - lea rax, title_str ; load address of title string in RAX register - ret + lea rax, title_string + ret get_title endp -; ASM Main function public asm_main -asm_main proc +asm_main proc - sub rsp, 56 - - lea rcx, prompt ; load prompt string address into RCX register - call printf ; this is the reigster where ABI tells us to place arguments + sub rsp, 56 - mov input, 0 ; ensure the input string is terminated with 0 + ; first we want to print a prompt, to do this we use the C++ function printf + ; we load this prompt in RCX and then call + lea rcx, prompt_string + call printf - lea rcx, input ; load input address to rcx register - mov rdx, maxlen ; move maxlen into rdx register - call readline ; use rcx and rdx as arguments to call readline + mov input, 0 - lea rcx, fmt_string - lea rdx, input - call printf + ; read user input + lea rcx, input + mov rdx, maxlen + call readline - add rsp, 56 + lea rcx, format_string + lea rdx, input + call printf + + add rsp, 56 ret asm_main endp - end + end \ No newline at end of file diff --git a/src/build.bat b/src/build.bat index b97e77f..5c6f0c9 100644 --- a/src/build.bat +++ b/src/build.bat @@ -6,4 +6,4 @@ REM /c is compile dont link REM /Zi is debug info REM /Cp is preserve cases ml64 /nologo /c /Zi /Cp %1.asm -cl /nologo /O2 /Zi /utf-8 /EHa /Fe%1.exe main.cpp %1.obj \ No newline at end of file +cl /nologo /O2 /Zi /utf-8 /EHa /Fe%1.exe c.cpp %1.obj \ No newline at end of file diff --git a/src/c.cpp b/src/c.cpp index f2c8219..83c72da 100644 --- a/src/c.cpp +++ b/src/c.cpp @@ -8,7 +8,6 @@ extern "C" { void asm_main(void); - char *get_title(void); int readline(char* dest, int maxlen);