start writting examples

This commit is contained in:
ergz 2022-06-22 13:09:41 -07:00
commit 13cbecb0e9
5 changed files with 119 additions and 0 deletions

19
src/1_8.asm Normal file
View File

@ -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 (?)

43
src/c.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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!")
}
}

27
src/hello.asm Normal file
View File

@ -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

13
src/main.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
extern "C"
{
void asm_func(void);
};
int main(void)
{
printf("calling asm main\n");
asm_func();
printf("return from asm main\n");
}

17
src/program.asm Normal file
View File

@ -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