为什么我在这个基本的装配程序中通过jmp?

时间:2015-02-24 21:18:02

标签: assembly nasm x86-64

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

我正在使用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时,这个东西会输出&#34; FALSE&#34;和退出 - 工作!然而,当A = 7且B = 5时,它打印出&#34; TRUE FALSE&#34;在退出之前...它怎么会忽略我的&#34; jmp退出&#34;调用put之后的语句打印出&#34; TRUE&#34;?

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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