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
|
option casemap:none
|
||||||
|
|
||||||
; constants
|
; constants
|
||||||
nl = 10
|
nl = 10 ; newline
|
||||||
maxlen = 256
|
maxlen = 256 ; max len of the string
|
||||||
|
|
||||||
.data
|
.data
|
||||||
|
|
||||||
title_str byte "Listing 1-8", 0
|
title_string byte 'Listing 1_8', 0
|
||||||
prompt byte "Enter a string: ", 0
|
prompt_string byte 'Enter a string: ', 0
|
||||||
fmt_string byte "User entered: '%s'", nl, 0
|
format_string byte 'User entered: "%s" ', nl, 0
|
||||||
|
|
||||||
; make `maxlen` copies/duplicates of size byte, each uinitialized
|
|
||||||
input byte maxlen dup (?)
|
|
||||||
|
|
||||||
|
input byte maxlen dup (?) ; make maxlen copies of byte, each uninitialized
|
||||||
|
|
||||||
.code
|
.code
|
||||||
|
|
||||||
|
@ -23,39 +18,37 @@ input byte maxlen dup (?)
|
||||||
externdef readline:proc
|
externdef readline:proc
|
||||||
|
|
||||||
public get_title
|
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
|
get_title proc
|
||||||
|
|
||||||
lea rax, title_str ; load address of title string in RAX register
|
lea rax, title_string
|
||||||
ret
|
ret
|
||||||
|
|
||||||
get_title endp
|
get_title endp
|
||||||
|
|
||||||
; ASM Main function
|
|
||||||
public asm_main
|
public asm_main
|
||||||
asm_main proc
|
asm_main proc
|
||||||
|
|
||||||
sub rsp, 56
|
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
|
|
||||||
|
|
||||||
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 input, 0
|
||||||
mov rdx, maxlen ; move maxlen into rdx register
|
|
||||||
call readline ; use rcx and rdx as arguments to call readline
|
|
||||||
|
|
||||||
lea rcx, fmt_string
|
; read user input
|
||||||
lea rdx, input
|
lea rcx, input
|
||||||
call printf
|
mov rdx, maxlen
|
||||||
|
call readline
|
||||||
|
|
||||||
add rsp, 56
|
lea rcx, format_string
|
||||||
|
lea rdx, input
|
||||||
|
call printf
|
||||||
|
|
||||||
|
add rsp, 56
|
||||||
ret
|
ret
|
||||||
|
|
||||||
asm_main endp
|
asm_main endp
|
||||||
|
|
||||||
end
|
end
|
|
@ -6,4 +6,4 @@ REM /c is compile dont link
|
||||||
REM /Zi is debug info
|
REM /Zi is debug info
|
||||||
REM /Cp is preserve cases
|
REM /Cp is preserve cases
|
||||||
ml64 /nologo /c /Zi /Cp %1.asm
|
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