Compare commits
No commits in common. "9eeacd4206c57014d53d22d17083a578b01d10ae" and "ad9f04fcc4970a7ce2df01016e456a11590c5994" have entirely different histories.
9eeacd4206
...
ad9f04fcc4
|
@ -2,4 +2,3 @@
|
||||||
*.pdb
|
*.pdb
|
||||||
*.obj
|
*.obj
|
||||||
*sublime-workspace
|
*sublime-workspace
|
||||||
*.exe
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
This repo is me working through the book The Art of 64 Bit Assembly.
|
|
||||||
To build listings in the `src/` folder simply call the `build.bat`
|
|
||||||
script with the listing number:
|
|
||||||
|
|
||||||
```batch
|
|
||||||
build 1_8 // don't add .asm extension
|
|
||||||
|
|
||||||
```
|
|
Binary file not shown.
13
src/1_3.asm
13
src/1_3.asm
|
@ -1,13 +0,0 @@
|
||||||
.CODE
|
|
||||||
|
|
||||||
option casemap:none ; make all symbols case sensitive (like C)
|
|
||||||
|
|
||||||
public asm_func ; make this visible outside of this source
|
|
||||||
|
|
||||||
asm_func PROC ; start procedure
|
|
||||||
|
|
||||||
ret
|
|
||||||
|
|
||||||
asm_func ENDP ; end procedure
|
|
||||||
|
|
||||||
END
|
|
15
src/1_4.asm
15
src/1_4.asm
|
@ -1,15 +0,0 @@
|
||||||
; a user defined proc
|
|
||||||
|
|
||||||
.code
|
|
||||||
|
|
||||||
asm_proc proc
|
|
||||||
ret ; return from the procedure
|
|
||||||
asm_proc endp
|
|
||||||
|
|
||||||
main proc ; the main procedure
|
|
||||||
call asm_proc ; call our proc
|
|
||||||
ret
|
|
||||||
main endp
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
21
src/1_5.asm
21
src/1_5.asm
|
@ -1,21 +0,0 @@
|
||||||
option casemap:none
|
|
||||||
|
|
||||||
.data
|
|
||||||
|
|
||||||
fmt_str byte 'Hello World!', 10, 0 ; here 10 is the newline feed
|
|
||||||
|
|
||||||
.code
|
|
||||||
|
|
||||||
externdef printf:proc
|
|
||||||
|
|
||||||
public asm_func
|
|
||||||
|
|
||||||
asm_func proc
|
|
||||||
sub rsp, 56
|
|
||||||
lea rcx, fmt_str ; load the format string into the RCX register to be used by the printf
|
|
||||||
call printf
|
|
||||||
add rsp, 56
|
|
||||||
ret
|
|
||||||
asm_func endp
|
|
||||||
|
|
||||||
end
|
|
56
src/1_8.asm
56
src/1_8.asm
|
@ -1,59 +1,59 @@
|
||||||
|
; assembly that shows how to return values to a
|
||||||
|
; a C function
|
||||||
|
|
||||||
option casemap:none
|
option casemap:none
|
||||||
|
|
||||||
; constants
|
; constants
|
||||||
nl = 10 ; newline
|
nl = 10
|
||||||
maxlen = 256 ; max len of the string
|
maxlen = 256
|
||||||
|
|
||||||
.data
|
.data
|
||||||
|
|
||||||
title_string byte 'Listing 1_8', 0
|
title_str byte "Listing 1-8", 0
|
||||||
prompt_string byte 'Enter a string: ', 0
|
prompt byte "Enter a string: ", 0
|
||||||
format_string byte 'User entered: "%s" ', nl, 0
|
fmt_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
|
||||||
|
|
||||||
externdef printf:proc
|
externdef printf:proc
|
||||||
externdef readline:proc
|
externdef readline:proc
|
||||||
|
|
||||||
|
|
||||||
;; get_title procedure -------------------------
|
|
||||||
;; get_title moves the address of the byte containing the string "Listing 1_8", 0
|
|
||||||
;; into the RAX register, it does this usig the LEA instruction
|
|
||||||
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_string ; load address of title_string into rax
|
lea rax, title_str ; load address of title string in RAX register
|
||||||
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
|
||||||
|
|
||||||
;; first we want to print a prompt, to do this we use the stdio function printf,
|
lea rcx, prompt ; load prompt string address into RCX register
|
||||||
;; we need to make the arguments for printf avaialble by loading the address of the
|
call printf ; this is the reigster where ABI tells us to place arguments
|
||||||
;; byte prompt_string into RCX
|
|
||||||
lea rcx, prompt_string
|
|
||||||
call printf
|
|
||||||
|
|
||||||
mov input, 0 ;; clear the input
|
mov input, 0 ; ensure the input string is terminated with 0
|
||||||
|
|
||||||
; read user input by calling readline from out cpp program. We load the required
|
lea rcx, input ; load input address to rcx register
|
||||||
; arguments into the registers RCX, and RDX and then call readline
|
mov rdx, maxlen ; move maxlen into rdx register
|
||||||
lea rcx, input
|
call readline ; use rcx and rdx as arguments to call readline
|
||||||
mov rdx, maxlen
|
|
||||||
call readline
|
|
||||||
|
|
||||||
; load RCX and RDX with arugments to printf, to print back to the user
|
lea rcx, fmt_string
|
||||||
lea rcx, format_string
|
lea rdx, input
|
||||||
lea rdx, input
|
call printf
|
||||||
call printf
|
|
||||||
|
|
||||||
add rsp, 56
|
add rsp, 56
|
||||||
ret
|
ret
|
||||||
|
|
||||||
asm_main endp
|
asm_main endp
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,9 +1,4 @@
|
||||||
@echo off
|
@echo off
|
||||||
echo ~~~~ starting build ~~~~
|
echo ~~~~ starting build ~~~~
|
||||||
|
|
||||||
REM /nolog is dont show MS info at run
|
|
||||||
REM /c is compile dont link
|
|
||||||
REM /Zi is debug info
|
|
||||||
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 c.cpp %1.obj
|
cl /nologo /O2 /Zi /utf-8 /EHa /Fe%1.exe c.cpp %1.obj
|
|
@ -8,7 +8,7 @@ extern "C"
|
||||||
{
|
{
|
||||||
void asm_main(void);
|
void asm_main(void);
|
||||||
|
|
||||||
// function is defined in the asm file
|
|
||||||
char *get_title(void);
|
char *get_title(void);
|
||||||
|
|
||||||
int readline(char* dest, int maxlen);
|
int readline(char* dest, int maxlen);
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
listing_1_9.cpp
|
|
11
src/main.cpp
11
src/main.cpp
|
@ -2,15 +2,12 @@
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
// Function written in ASM
|
|
||||||
void asm_func(void);
|
void asm_func(void);
|
||||||
}
|
};
|
||||||
|
|
||||||
int main()
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("calling asm function\n");
|
printf("calling asm main\n");
|
||||||
asm_func();
|
asm_func();
|
||||||
printf("returned from asm function\n");
|
printf("return from asm main\n");
|
||||||
|
|
||||||
return(0);
|
|
||||||
}
|
}
|
Binary file not shown.
BIN
src/mllink$.lnk
BIN
src/mllink$.lnk
Binary file not shown.
Loading…
Reference in New Issue