$\require{cancel}$
; asm global PRINT_HELLO ... section .data HELLO_STRING db `Hello from Assembly!\n`,0 helloLen equ $ - HELLO_STRING ... PRINT_HELLO: push rbp mov rbp, rsp mov rdi, HELLO_STRING mov rsi, helloLen call PRINT_STRING pop rbp ret // c++ extern "C" void PRINT_HELLO(); ... PRINT_HELLO();
// c++ extern "C" void PRINT_STRING(const char phrase[], int size); ... string message{"Hello from C++/from Assembly!\n"}; ... PRINT_STRING(message.c_str(), message.size());
; nasm global DOUBLE_NUMBER ... ; rdi holds the number ; return value in rax DOUBLE_NUMBER: mov rax, rdi add rax, rax ret // c++ int number{100}; ... number = DOUBLE_NUMBER(number);
; nasm ; rdi holds the address of the number; ; at first I was using r8, but crashed on return ; I was over writing memory that was not part of the variable. DOUBLE_IN_PLACE: mov r8d, DWORD [rdi] add r8d, r8d mov DWORD [rdi], r8d ret // c++ extern "C" int DOUBLE_IN_PLACE(int & number); ... DOUBLE_IN_PLACE(number);
; rdi holds the address of the array ; rsi holds the size of the array ; rdx holds the fill value FILL_ARRAY: push rbp mov rbp, rsp push r15 ; lcv push r14 ; offset mov r15, 0 mov r14, 0 .top: cmp r15, rsi je .done mov [rdi+r14], rdx add r14, 4 inc r15 jmp .top .done: pop r14 pop r15 pop rbp ret // c++ extern "C" void FILL_ARRAY(int array[], int size, int fillvalue); ... iint values[SIZE]; ... FILL_ARRAY(values, SIZE, 100);