commit 13cbecb0e9411e1501a8da142b5f0b4486127dc5 Author: ergz Date: Wed Jun 22 13:09:41 2022 -0700 start writting examples diff --git a/src/1_8.asm b/src/1_8.asm new file mode 100644 index 0000000..02a726a --- /dev/null +++ b/src/1_8.asm @@ -0,0 +1,19 @@ +; assembly that shows how to return values to a +; a C function + + option casemap:none + +; constants +nl = 10 +maxlen = 256 + + .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 (?) + + diff --git a/src/c.cpp b/src/c.cpp new file mode 100644 index 0000000..fed16fc --- /dev/null +++ b/src/c.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include + + +extern "C" +{ + void asm_main(void); + + + char *get_title(void); + + int readline(char* dest, int maxlen); +} + +int readline(char *dest, int maxlen) +{ + char *result = fgets(dest, maxlen, stdin); + if (results != NULL) { + int len = strlen(result); + if (len > 0) { + dest[len - 1] = 0; + } + return(len); + } + + return(-1) // an error occured +} + +int main(void) +{ + + try { + char *title = get_title(); + printf("calling %s\n", title); + asm_func(); + printf("%s terminated\n", title); + + } catch(...) { + printf("an error occured!") + } +} \ No newline at end of file diff --git a/src/hello.asm b/src/hello.asm new file mode 100644 index 0000000..a94f998 --- /dev/null +++ b/src/hello.asm @@ -0,0 +1,27 @@ +; a hello world program using printf from C + + option casemap:none + + .data + +format_string byte "Hello from asm world!", 10, 0 + + .code + + externdef printf:proc + + public asm_func + +asm_func proc + + sub rsp, 56 + lea rcx, format_string + call printf + add rsp, 56 + + ret + +asm_func endp + + end + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..51dad6d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,13 @@ +#include + +extern "C" +{ + void asm_func(void); +}; + +int main(void) +{ + printf("calling asm main\n"); + asm_func(); + printf("return from asm main\n"); +} \ No newline at end of file diff --git a/src/program.asm b/src/program.asm new file mode 100644 index 0000000..c94f409 --- /dev/null +++ b/src/program.asm @@ -0,0 +1,17 @@ +; comments start with a colon + + + .CODE + + option casemap:none + + public asm_func + +asm_func PROC + + ret ; returns to the caller + +asm_func ENDP + + END +