2015-02-24 14 views
2

爲了提出這個非常簡單(我確定)的問題,提前道歉。爲什麼我在這個基本的彙編程序中通過jmp失敗?

我使用的是NASM彙編程序,並有一個英特爾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,這件事打印出 「假」 並退出 - 作品!但是,當A = 7和B = 5時,在退出之前它會打印出「TRUE FALSE」...在放入打印出「TRUE」之後,它如何忽略我的「jmp exit」語句?

回答

3

你的字符串必須是NULL結尾的,這樣puts會知道每個字符串結尾:

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

謝謝!它現在有效。 – Amy 2015-02-24 21:24:59

1

好了,不知道你的看跌期權,但假設它打印,直到它看到一個0

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