为什么我在这个基本的汇编程序上通过 jmp 失败了?

Why am I falling through jmp on this basic assembly program?

提前为提出这个非常简单(我确定)的问题道歉。

我正在使用 NASM 汇编程序并且有一个 intel i5 处理器...如果相关的话...另外,请随意忽略我对自己所做的代码注释...或评论在我的评论中,无论哪种方式。 :)

这是我的代码:

; test_if_nasm.asm - if/then/else in NASM assembly language

bits 64
global main
extern puts

section .data
    A    dd    7
    B    dd    5
    LC0  db    "TRUE "
    LC1  db    "FALSE "

section .text
main:
; function setup
    push    rbp         ; set up stack frame??
    mov     rbp, rsp    ; copy rsp into rbp... what is in rsp???
    sub     rsp, 32     ; subtract 32 from value in rsp & save result
                        ; in rsp... but why?

; user code goes here
    mov     edx, [A]    ; We'll see
    mov     eax, [B]    ; copy value referenced by B into eax
    cmp     edx, eax    ; compare edx to eax
    jle     printFalse  ; if edx <= eax, go to L2
    mov     edi, LC0    ; executes if eax > edx, move LC0 ("T") into edi
    call    puts        ; print... what's in edi (LC0)... right?
    jmp     exit        ; ensures exit after printing "TRUE"

printFalse:
    mov     edi, LC1    ; copy LC1 ("F") into edi
    call    puts        ; print ... what's in edi (LC1)... right?
    jmp     exit        ; don't go back and also print out true

; function return

exit:                   ; Other than this being a return function
    mov    eax, 0       ; I have not one single clue what is going
    add    rsp, 32      ; on here or why.
    pop    rbp
    ret                 ; Pretty sure this means return. Woohoo!

好的,这是我的问题:

当 A = 5 和 B = 7 时,这个东西打印出 "FALSE" 并退出 - 有效!但是,当 A = 7 和 B = 5 时,它会在退出前打印出 "TRUE FALSE "...为什么它会在调用 puts 后忽略我的 "jmp exit" 语句并打印出 "TRUE"?

您的字符串需要以 NUL 结尾,以便 puts 知道每个字符串的结尾位置:

LC0  db    "TRUE ",0
LC1  db    "FALSE ",0

嗯,不知道你的 puts,但假设它打印直到它看到 0。

您的 TRUE/FALSE 字符串中缺少它...