2012-03-21 59 views
0

我有一個任務,我們必須編寫兩個函數。還必須使用處理器的條件代碼檢測溢出條件並返回0以指示已遇到錯誤。我能夠編寫這些功能。如何檢測裝配過程中的溢出情況Langauge X86

.file "formula.c" 
    .text 
.globl _nCr 
    .def _nCr; .scl 2; .type 32; .endef 
_nCr: 
     pushl %ebp 
    movl %esp, %ebp 
    subl $56, %esp 
    movl 8(%ebp), %eax 
    movl %eax, (%esp) 
    testl %eax, %eax 
    call _factorial 
    movl %eax, -12(%ebp) 
    movl 12(%ebp), %eax 
    addl $1, %eax 
    movl %eax, (%esp) 
    call _factorial 
    movl %eax, -16(%ebp) 
    movl 12(%ebp), %eax 
    notl %eax 
    addl 8(%ebp), %eax 
    movl %eax, (%esp) 
    call _factorial 
    movl %eax, -20(%ebp) 
    movl -16(%ebp), %eax 
    movl %eax, %edx 
    imull -20(%ebp), %edx 
    movl %edx, -28(%ebp) 
    movl -12(%ebp), %eax 
    movl %eax, %edx 
    sarl $31, %edx 
    idivl -28(%ebp) 
    leave 
    ret 
.globl _factorial 
    .def _factorial; .scl 2;  .type 32;  .endef 
_factorial: 
    pushl %ebp 
    movl %esp, %ebp 
    subl $16, %esp 
    movl $1, -8(%ebp) 
    movl $1, -4(%ebp) 
    jmp L3 
L4: 
    movl -8(%ebp), %eax 
    imull -4(%ebp), %eax 
    movl %eax, -8(%ebp) 
    addl $1, -4(%ebp) 
L3: 
    movl -4(%ebp), %eax 
    cmpl 8(%ebp), %eax 
    jle L4 
    movl -8(%ebp), %eax 
    leave 
    ret 
    .def ___main; .scl 2; .type 32; .endef 
    .section .rdata,"dr" 
    .align 4 

這個功能基本上沒有n!/r!(n-r)!。當數字變大時,溢出發生在階乘。我只是不明白我將如何設置溢出條件。

+2

溢出條件由算術指令自動設置。你只需要知道如何閱讀它們。 – Mysticial 2012-03-21 01:34:58

+0

感謝您的回覆。我應該把我自己的條件標誌。我只是遇到了麻煩,我該如何放入自己的狀態標誌。 – user1282285 2012-03-21 01:38:07

+0

基於這個問題和你的評論,看起來這是家庭作業,所以我添加了作業標籤。如果這不是作業,請隨時移除標籤。 – 2012-03-21 01:41:55

回答

2

1)你的算術命令是可能會設置溢出位

2)溢出的「JO」(跳轉)和「JNO」上不溢出(跳)允許您分支操作,這取決於關於是否發生溢出

3)您可能在「JO」之後將「%eax」設置爲0。

4)優秀的,優秀的資源,如果你不熟悉的人:

Programming from the Ground Up, Jonathan Bartlett

+0

非常感謝! JO非常有幫助。而且也是資源。 – user1282285 2012-03-21 02:09:59

1

在x86架構中,當一個算術指令執行諸如addl 8(%ebp), %eax條件碼在CPU中設置狀態字。有些指令的行爲取決於條件代碼。

您可以讓代碼在給定條件下采用替代路徑(執行分支)。 x86在Jxx助記符下有一系列條件分支指令:JA, JAE, JB, JBE, JC, JCXZ, ..., JZ。例如JZ表示如果爲零則跳轉:如果指令產生零結果,則設置零標誌。 JO跳躍溢出。

一個條件也可以轉換爲一個字節數據並存儲到寄存器或內存中。這是編譯像C表達式有用:

x = (y != 3); /* if (y != 3) x = 1; else x = 0 */ 

它是由SETx組這也是衆多的指示做的,就像條件分支:SETA, SETAE, SETB, ..., SETZ。例如,如果零條件爲真,則SETZ將給定字節設置爲1。例如。

seto %bl /* set bottom byte of B register to 1 if overflow flag set */