2016-10-14 156 views
0

我已将此代码写入emu8086。
当我按下模拟时,编译代码需要很长时间,编译时它的工作原理不正确。 (而不是跳到主它跳到最大功能等)emu8086 - 编译需要很长时间并且程序不起作用

而在你说可能有一个“magshimim.inc”的问题,没有,它在其他文件中的作品。

include magshimim.inc 

org 100h 

jmp main 

;--------------------------------------------; 
; Functions 
;--------------------------------------------; 

; This function gets 2 numbers and an address. 
; It stores the biggest number in the address. 
; Input: 
; push result_address 
; push num1 
; push num2 
PROC max 

    ; store offset of parameters relative to bp 
    result_p equ  6 
    num1  equ  4 
    num2  equ  2 

    push bp  ; store the previous stack frame 
    mov  bp, sp ; create new stack frame 
    push ax  ; store ax 

    mov ax, [bp+num1] 
    cmp ax, [bp+num2] 
    jng num1_bigger_num2 

    num1_bigger_num2: 
     mov ax, [bp+num1] 
     mov [[bp+result_p]], ax 
     jmp skip1 

    num1_not_bigger_num2: 
     mov ax, [bp+num2] 
     mov [[bp+result_p]], ax 

    skip1: 

    pop  ax  ; re-store ax 
    mov  sp, bp ; close stack frame 
    pop  bp  ; re-store the previous stack frame 

ret 
ENDP 


;--------------------------------------------; 
; Global variables 
;--------------------------------------------; 

    result  dw 0 
    num0  dw 2 
    num1  dw 10 

;--------------------------------------------; 
; Main 
;--------------------------------------------; 

main: 

    push offset result 
    push num0 
    push num1 
    call max 
    add sp, 6 

    mov ax, result 
    call print_num 

    mov ah, 0 
    int 16h 

ret 
+0

我刚刚在我的EMU中运行了您的代码,并且一旦启动就立即跳转到主代码。 –

+0

我认为这是我的电脑的问题。 – tomgrin10

+0

执行此操作:将程序“max”剪下并将其粘贴到最终“ret”下方。 –

回答

1
; store offset of parameters relative to bp 
result_p equ  6 
num1  equ  4 
num2  equ  2 

因为你使用了错误的偏移量来检索程序参数,你的程序不工作!
当指令mov ax, [bp+num1]执行堆栈包含以下(从低地址向高地址):

Old AX 
     Old BP 
    ^ Return Address 
     |     Value 10 
     |       Value 2 
Current BP points to here!    Offset result 
     |----> +2 
     |------------------> +4 
     |--------------------------> +6 
     |---------------------------------> +8 

这导致这些校正相当于:

result_p equ  8 
num1  equ  6 
num2  equ  4 

mov ax, [bp+num1] 
cmp ax, [bp+num2] 
jng num1_bigger_num2 
num1_bigger_num2: 

这是第二个问题。当比较的结果是大于时,您在下面的代码中遇到了问题,但当结果是不大于时,您跳转到自我相同的代码!这显然是行不通的。解决方案是跳转到_num1_not_bigger_num2_标签。

mov ax, [bp+num1] 
    cmp ax, [bp+num2] 
    jng num1_not_bigger_num2 <-- corrected 
num1_bigger_num2: 
    mov ax, [bp+num1] 
    mov [bp+result_p], ax  <-- corrected 
    jmp skip1 
num1_not_bigger_num2: 

mov [[bp+result_p]], ax 

我不知道为什么EMU8086会接受这些多余的方括号。最好只使用单个对[]时寻址内存。


为了让您的生活更轻松,您应该在命名变量时保持一致。
主要一部分,你必须按顺序:

num1, num0, offset result 

但在PROC你必须按顺序:

num2, num1, result_p 

这是非常混乱,容易出错!


include magshimim.inc 
org 100h 
jmp main 

您说过有这个包含文件没有问题,但我会建议你把包括jmp main指令之下。 org 100h表示您正在编译.COM文件,并且jmp main必须是执行路径上的第一条指令。您不希望包含文件中的任何指令来到此重要的jmp main之前。

org 100h 
jmp main 
include magshimim.inc