Assembly x64 Intro - Condition Compare

本文介绍了一个使用nasm汇编语言实现的简单条件判断程序。该程序通过比较两个长整型变量的大小来演示if-else语句的用法,并通过调用printf函数输出比较结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




; ifint_64.asm  code ifint_64.c for nasm
; /* ifint_64.c an 'if' statement that will be coded for nasm */
; #include <stdio.h>
; int main()
; {
;   long int a=1;
;   long int b=2;
;   long int c=3;
;   if(a<b)
;     printf("true a < b \n");
;   else
;     printf("wrong on a < b \n");
;   if(b>c)
;     printf("wrong on b > c \n");
;   else
;     printf("false b > c \n");
;   return 0;
;}
; result of executing both "C" and assembly is:
; true a < b
; false b > c
 
 global main  ; define for linker
        extern printf  ; tell linker we need this C function
        section .data  ; Data section, initialized variables
a: dq 1
b: dq 2
c: dq 3
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

 section .text
main: push rbp  ; set up stack
 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");
        call    printf 
        jmp exit1  ; jump over false part
false1: ;  a < b is false
        mov rdi, fmt2 ; printf("wrong on a < b \n");
        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");
        call    printf 
        jmp exit2  ; jump over false part
false2: ;  b > c is false
        mov rdi, fmt4 ; printf("false b > c \n");
        call    printf
exit2:    ; finished 'if' statement

 pop rbp  ; restore stack
 mov rax,0  ; normal, no error, return value
 ret   ; return 0;








true a < b
false b > c


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值