2016-12-09 109 views
-1

我有一个问题,我的循环,它包含的代码很长,它给了我错误jump destination too far : by 3 byte(s)。当删除:跳转目的地太远:通过3个字节

mov edx,offset str1 
call writestring 

这部分低于主PROC,它没有给出错误。但是我需要这个字符串用户输入一个负数来给出一条消息。我怎么能够?

INCLUDE Irvine32.inc 

.data 

    money  dword 200,100,50,20,10,5,1 
    str1  byte  "Enter the amounts for each value of money : ",0 
    str2  byte  "The sum of your moneys are:",0 
    total  dword 0 
    buffer  dword 1000 dup(0),0  
    str3  byte  "Do not enter neg number ",0 

.code 
main PROC 
    mov edx,offset str1 
    call writestring 
    call crlf 
    mov ecx,lengthof money 
    mov esi,0 
    mov edi,0 

start1: 
    jmp continue 
    don: 
    push ecx 


    mov edx,ecx 
    mov edx,0 

    mov edx,7 
    sub edx,ecx 
    mov ecx,edx 
    mov edi,0 
    mov esi,0 
     start2: 

      mov eax,money[esi] 
      call writedec 
      mov ebx,eax 
      mov al,'x' 
      call writechar 
      mov eax,buffer[edi] 
      call writedec 
      call crlf 
      add esi,4 
      add edi,4 

     loop start2 

    pop ecx 
    continue: 

    ;************************************************** 
    mov edx,0 
    mov eax,money[esi] 
    call writedec 
    mov ebx,eax 
    mov al,'x' 
    call writechar 
    call readint 
    ;*************************************************** 

    sub eax,0 
    js don 
    mov buffer[edi],eax 
    ;************************* 
    mul ebx 
    add total,eax  ;we add each the multiplication to total. 
    add esi,4   ;increases the index by 4.(because of dword type) 
    add edi,4 


loop start1 

    mov edx,offset str2 
    call writestring 
    mov eax, total 
    call writedec 

    exit 
main ENDP 
END main 
+0

该错误指的是什么指令? – duskwuff

+0

mov edx,offset str1 call writestring – zahit

+1

你的代码是你永远不应该使用LOOP指令的一个很好的例子。 –

回答

1

loop的范围有限。从下一条指令开始测量的指令流中,它只能跳到先前的127个字节或128个字节。

为了解决这个问题,你可以做如下的事情。基于不具备的条件跳转

label1: 

<lots of code> 

loop tmp1 
jmp tmp2 
tmp1: 
    jmp label1 
tmp2: 

或者使用不同的结构:

而不是

label1: 

<lots of code> 

loop label1 

如果标签是遥不可及,你可以做这样的事情范围限制。

+0

非常感谢。那么错误取决于跳转指令的类型,例如js?或者我只需要放标签? – zahit

+0

'js'和其他条件跳转_used_在早期x86模型上具有相同的限制,但现在永远有两种变体,一种是有符号的32位分支范围。大多数汇编程序只是从助记符中自动选择正确的跳转大小。 –

+3

为什么不直接显示'dec ecx/jnz label1'作为替代方案,而不是使用LOOP和JMP这种可怕的难读结构?顺便说一句,JCC rel32至少早在386被支持,所以你永远不需要在32位代码中避免它。 –