%include "IO_DEFS.asm" section .data num dd 24601 ; we will discuss db soon, but this does num = 42 section .bss buffer resb 16 ; this rese r ves 16 bytes (or characters) ; the same as char buffer[16] in c. section .text global _start ; data dictionary ; All work is done in 32 bit registers. ; eax holds the number to be printed, or some version of it. ; edi holds the address of the buffer where we will store the number ; edx and ecx are used for temporary computations. ; edx is the digit just removed fom eax ; dl will contain the character we are interested in ; ecx is 10 ; ; num = 24601; ; char buffer[16] ; buffer[16] = 0x0A (or '\n') ; (pos = 15) ; while (num > 0) { ; digit = num % 10 ; num = num / 10 ; buffer[pos] = static_cast(digit + '0') ; (--pos) ; } _start: mov eax, [num] ; load the number from memory lea edi, [buffer + 15] ; load effective address, base address + 15 ; edi = buffer+15 ; Carter 93. mov byte [edi], 0x0A ; newline character buffer[15] = '\n' convert: dec edi ; edi-- Carter page 23 mov edx, 0 ; edx = 0 mov ecx, 10 ; ecx = 10 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 convert ; ***************************************************************** ; Print the result ; ***************************************************************** ; remember, at this point edi holds the last position we wrote to. ; or the start of the string in the buffer. ; this will vary based on the number of digits mov esi, edi ; edi needs the start position of the string. lea edx, [buffer+16] ; 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 ; Exit mov eax, SYS_EXIT mov edx, SUCCESS syscall