STDIN: EQU 0 STDOUT: EQU 1 STDERR: EQU 2 SYS_READ: EQU 0 SYS_WRITE: EQU 1 section .data PRINT_ENDL db `\n`,0 section .bss PRINT_INT_buffer resb 16 section .text global PRINT_INT global PRINT_NEWLINE global PRINT_STRING ; rdi will hold the address of the string to print ; rsi will hold the length of the string to print PRINT_STRING: mov rdx, rsi mov rsi, rdi mov rax, STDOUT mov rdi, SYS_WRITE syscall ret PRINT_NEWLINE: mov eax, SYS_WRITE mov edi, STDOUT ; set the destination to stdout mov esi, PRINT_ENDL mov edx, 2 syscall ret PRINT_INT: ; this is a leaf routine, so we don't need to mess with the stack. ; Parameter: rdi, the number to print mov rax, rdi mov [PRINT_INT_buffer+15], byte 0 lea edi, [PRINT_INT_buffer + 14] PRINT_INT_convert: dec edi ; edi scratch mov edx, 0 ; edx scratch mov ecx, 10 ; ecx scratch div ecx ; eax = edx:eax/ecx edx = edx:eax%ecx ; carter page 44 add dl, '0' ; dl + '0' add ascii 0 to the digit mov [edi], dl ; store the new character in edi buffer[14] = digit test eax, eax ; check for any bits of eax not zero ; carter page 51 jnz PRINT_INT_convert mov esi, edi ; esi needs the start position of the string. lea edx, [PRINT_INT_buffer+14] ; edx needs the length of the string ; the last character in the buffer. sub edx, edi ; starting poisition - end position mov eax, SYS_WRITE mov edi, STDOUT ; set the destination to stdout syscall ret