[list -] %INCLUDE "Along32.inc" %INCLUDE "Macros_Along.inc" [list +] ;--------------------------------------------------------------------- extern fflush ; HLL prototype: int fflush(FILE *stream); ; For output streams (and for update streams on which the last ; operation was output), writes any unwritten data from the stream's ; buffer to the associated output device. ; ; For input streams (and for update streams on which the last ; operation was input), the behavior is undefined. ; ; If stream is a null pointer, all open output streams are flushed, ; including the ones manipulated within library packages or otherwise ; not directly accessible to the program. ; Receives: stream - the file stream to write out ; Returns: Returns zero on success. Otherwise EOF is returned and the ; error indicator of the file stream is set. ; Source: https://en.cppreference.com/w/c/io/fflush ;--------------------------------------------------------------------- ;--------------------------------------------------------------------- extern printf ; HLL prototype: int printf(const char *format, ...); ; Loads the data from the given locations, converts them to character ; string equivalents and writes the results to the output stream ; stdout. ; Receives: format - pointer to a null-terminated byte string ; specifying how to interpret the data ; ... - arguments specifying data to print. If any ; argument after default argument promotions is ; not the type expected by the corresponding ; conversion specifier, or if there are fewer ; arguments than required by format, the behavior ; is undefined. If there are more arguments than ; required by format, the extraneous arguments are ; evaluated and ignored. ; Returns: number of characters transmitted to the output stream or ; negative value if an output error or an encoding error ; (for string and character conversion specifiers) occurred ; Source: https://en.cppreference.com/w/c/io/fprintf ;--------------------------------------------------------------------- ;--------------------------------------------------------------------- extern GCD ; HLL prototype: int GCD(int x, int y); ; Returns the greatest common divisor (gcd) of two signed 32-bit ; integers. ; Implements the following pseudocode: ; int GCD(int x, int y) ; { ; x = abs(x); ; y = abs(y); ; if (x == 0) ; return y; ; else if (y == 0) ; return x; ; do ; { ; int n = x % y; ; x = y; ; y = n; ; } while (y > 0); ; return x; ; } ; Receives: two signed 32-bit integers via the system stack ; Returns: a 32-bit integer in EAX ;--------------------------------------------------------------------- SECTION .data fmt db `%s%11d%s%11d%s%10d\n`,0 fmt2 db `%s`,0 hrule times 44 db ('-') db 10,0 spacer2 times 2 db ' ' db 0 spacer4 times 4 db ' ' db 0 header db ` X Y GCD(X,Y)\n`,0 SECTION .bss h resd 1 x resd 1 y resd 1 SECTION .text global _start _start: call ReadDec ; read an unsigned integer mov [h],eax ; move the integer to h mov edx,hrule ; write hrule call WriteString mov edx,header ; write headings call WriteString mov edx,hrule ; write hrule call WriteString .L0: cmp dword [h],0 ; while h >= 0 do je .L1 call ReadInt ; read a signed 32-bit integer mov [x],eax ; save a copy in x call ReadInt ; read a second signed 32-bit integer mov [y],eax ; save a copy in y push dword [y] ; call GCD(x, y) push dword [x] call GCD add esp,8 ; clean up the system stack ; Make a call to the C function printf ; printf("%s%11d%s%11d%s%10d\n", spacer2, x, spacer4, y, spacer4, gcd); push eax ; the gcd is in EAX push spacer4 ; pointer to string spacer4 push dword [y] ; push the value of y push spacer4 ; pointer to string spacer4 push dword [x] ; push the value of x push spacer2 ; pointer to string spacer2 push fmt ; pointer to the format string call printf add esp,28 ; clean up the system stack dec dword [h] ; decrement h jmp .L0 ; end while .L1: ; Make a call to the C function fflush; if a null pointer is passed, all ; open output streams are flushed ; fflush(0); push 0 ; push NULL (0) pointer on stack call fflush add esp,4 ; clean up the system stack mov edx,hrule ; write hrule call WriteString Exit {0}