; =================================================================== ; Program: if_then_else_v3_nasm.asm ; Date: July 2020 ; Author: Jean-Michel Richer ; Email: jean-michel.richer@univ-angers.fr ; =================================================================== ; Description: ; This program shows how to code a if then else statement. ; This third version asks the values of 'a' and 'b' and ; then prints them ; =================================================================== global main extern printf extern scanf ; =================================================================== ; ##### ## ##### ## ; # # # # # # # ; # # # # # # # ; # # ###### # ###### ; # # # # # # # ; ##### # # # # # ; =================================================================== section .data a: dd 1 b: dd 2 msg_inf: db "%d < %d", 10, 0 msg_supe: db "%d >= %d", 10, 0 msg_ask: db "a, b ? ", 0 msg_scanf: db "%d%d", 0 ; =================================================================== ; #### #### ##### ###### ; # # # # # # # ; # # # # # ##### ; # # # # # # ; # # # # # # # ; #### #### ##### ###### ; =================================================================== section .text ; ------------------------------------------------------------------- ; SUBPROGRAM ; ; int main(int argc, char *argv[]) ; ; DESCRIPTION ; ; Main subprogram that compares variables 'a' and 'b' ; ; PARAMETERS ; ; argc int number of arguments of the program ; argv char *[] array of arguments as strings ; ; RETURN VALUE ; ; 0 if everything is ok, ; >0 if error ; ; VARIABLES / REGISTERS ; ; a int first operand eax ; b int second operand ebx ; ; ------------------------------------------------------------------- main: push ebp mov ebp, esp push dword msg_ask call printf add esp, 4 push dword b push dword a push dword msg_scanf call scanf add esp, 4 push ebx mov eax, [a] mov ebx, [b] .if: cmp eax, ebx jge .else .then: push ebx push eax push dword msg_inf call printf add esp, 12 jmp .endif .else: push dword [b] ; equivalent to push ebx push dword [a] ; equivalent to push eax push dword msg_supe call printf add esp, 12 .endif: pop ebx xor eax, eax ; 0 of return 0 mov esp, ebp pop ebp ret