adds user input program
This commit is contained in:
parent
6cccbf0a18
commit
24aabcf0a0
57
src/1_8.asm
57
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
|
|
@ -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
|
||||
cl /nologo /O2 /Zi /utf-8 /EHa /Fe%1.exe c.cpp %1.obj
|
Loading…
Reference in New Issue