; exnl5.asm example if then else extern printf section .data ; Data section, initialized variables msg: db "nasm exnl5.asm running", 0 ; C string needs 0 msg2: db "exnl5.asm finished", 0 msg3: db "if then else on integers", 0 msg4: db "if then else on floats", 0 fmt: db "%s", 10, 0 a: dq 1 b: dq 2 c: dq 3 xyz: dq 4 fmt1: db "true a < b ",10,0 fmt2: db "wrong on a < b ",10,0 fmt3: db "wrong on b > c ",10,0 fmt4: db "false b > c ",10,0 fmt5: db "failed 4==xyz ",10,0 fmt6: db "passed 4==xyz ",10,0 fmt7: db "failed 5>xyz ",10,0 fmt8: db "passed 5>xyz ",10,0 fmt9: db "failed 3 cf ",10,0 fmt4f: db "false bf > cf ",10,0 section .bss ; unused, except to pop t: rest 1 ; reserve one space for dt section .text ; Code section. global main main: push rbp ; set up stack frame, must be aligned mov rdi,fmt ; address of format, required register rdi mov rsi,msg ; address of data mov rax,0 ; or can be xor rax,rax call printf ; Call C function mov rdi,fmt ; address of format, required register rdi mov rsi,msg3 ; mov rax,0 ; call printf ; ; code ifint_64.c for nasm ; /* ifint_64.c an 'if' statement that will be coded for nasm */ ; #include ; int main() ; { ; long int a=1; ; long int b=2; ; long int c=3; ; long int xyz=4; ; if(ac) ; printf("wrong on b > c \n"); ; else ; printf("false b > c \n"); ; ; if(4==xyz) goto label1e; ; printf("failed 4==xyz\n"); ;label1e: printf("passed 4==xyz\n"); ; ; if(5>xyz) goto label1g; ; printf("failed 5>xyz\n"); ;label1g: printf("passed 5>xyz\n"); ; ; if(3 c mov rax,[a] ; a cmp rax,[b] ; compare a to b jge false1 ; choose jump to false part ; a < b sign is set mov rdi, fmt1 ; printf("true a < b \n"); mov rax,0 call printf jmp exit1 ; jump over false part false1: ; a < b is false mov rdi, fmt2 ; printf("wrong on a < b \n"); mov rax,0 call printf exit1: ; finished 'if' statement mov rax,[b] ; b cmp rax,[c] ; compare b to c jle false2 ; choose jump to false part ; b > c sign is not set mov rdi, fmt3 ; printf("wrong on b > c \n"); mov rax,0 call printf jmp exit2 ; jump over false part false2: ; b > c is false mov rdi, fmt4 ; printf("false b > c \n"); mov rax,0 call printf exit2: ; finished 'if' statement mov rax,4 cmp rax,[xyz] ; if(4==xyz) goto label1e; je label1e mov rdi, fmt5 mov rax,0 call printf label1e:mov rdi, fmt6 mov rax,0 call printf mov rax,5 cmp rax,[xyz] ; if(5>xyz) goto label1g; jg label1g mov rdi, fmt7 mov rax,0 call printf label1g:mov rdi, fmt8 mov rax,0 call printf mov rax,3 cmp rax,[xyz] ; if(3 ; int main() ; { ; double af=1.0; ; double bf=2.0; ; double cf=3.0; ; if(afc) ; printf("wrong on bf > cf \n"); ; else ; printf("false bf > cf \n"); ; return 0; ;} ; result of executing both "C" and assembly is: ; true af < bf ; false bf > cf fld tword [bf] ; bf into st0 fld tword [af] ; af into st0, pushes b into st1 fcompp ; compare and pop both ; fcomip st0,st1 ; compare af to bf, pop af ; fstp tword [t] ; just to pop bf jl false1f ; choose jump to false part ; af < bf sign is set mov rdi, fmt1f ; printf("true af < bf \n"); mov rax, 0 call printf jmp exit1f ; jump over false part false1f: ; af < bf is false mov rdi, fmt2f ; printf("wrong on af < bf \n"); mov rax, 0 call printf exit1f: ; finished 'if' statement fld tword [cf] ; cf into st0 fld tword [bf] ; bf into st0, pushes cf into st1 fcompp ; compare and pop both ; fcomip st0,st1 ; compare bf to cf, pop bf ; fstp tword [t] ; just to pop cf jg false2f ; choose jump to false part ; bf > cf sign is not set mov rdi, fmt3f ; printf("wrong on bf > cf \n"); mov rax, 0 call printf jmp exit2f ; jump over false part false2f: ; bf > cf is false mov rdi, fmt4f ; printf("false bf > cf \n"); mov rax, 0 call printf exit2f: ; finished 'if' statement mov rdi,fmt ; print finished mov rsi,msg2 mov rax,0 call printf pop rbp ; restore stack mov rax,0 ; normal, no error, return value ret ; return