【问题标题】:nasm infinite loop with FPU带有 FPU 的 nasm 无限循环
【发布时间】:2013-06-25 22:19:39
【问题描述】:

我正在尝试创建一个小型 nasm 程序,它在浮点中执行此操作

while(input <= 10^5) do
begin
   input = input * 10
   i = i - 1
end

nasm 中的等价程序如下

section .data

    input: resd 1
    n10: dd 0x41200000          ; 10

_start:
    mov eax, 0x43480000        ; eax = 200

    mov dword [input], eax      ; input = eax = 200
    mov edx, 0x49742400         ; 10^5

    ; %begin
    mov ecx, 0                  ; i = 0
    jmp alpha

alpha:
    cmp [input], edx            ; input <= 10^5
    jle _while                  
    jmp log2

_while:
    fld dword [input]            ; input
    fmul dword [n10]                ; input * 10
    fst dword [input]            ; input = input
    dec ecx                      ; i = i - 1
    jmp alpha

_while 循环无限迭代

ecx / i gards 总是相同的value = 0(它被设置为 0)并且不会递减

【问题讨论】:

    标签: nasm fpu


    【解决方案1】:

    这对我有用(在 DosBox 中测试):

    org 0x100
    bits 16
    
    _start:
    mov dword [input], __float32__(99.0)
    mov edx, __float32__(10000.0)  
    
    mov ecx, 0                  ; i = 0
    jmp alpha
    
    alpha:
    cmp [input],edx            ; input <= 10^5
    jle _while                  
    jmp log2
    
    _while:
    fld dword [input]            ; input
    fmul dword [n10]                ; input * 10
    fstp dword [input]            ; input = input
    inc ecx                      ; i = i - 1
    jmp alpha
    
    log2:
    
    ; print the value of cl
    mov dl,cl
    add dl,'0'
    mov ah,2
    int 21h
    
    ; Exit to DOS
    mov ah,0x4c
    int 21h
    
    n10: dd 10.0 
    input: resd 1
    

    注意bits 16,它告诉nasm 16 位操作数是默认的,使用32 位操作数的指令应该加上前缀。如果没有这个,如果您尝试在实模式环境中执行代码,您的代码将被视为乱码。
    您可能需要改用 bits 32,具体取决于您的目标环境。
    另请注意使用浮点文字而不是十六进制值(您的代码中有错字,您将其与 10^6 而不是 10^5 进行比较)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-31
      • 2021-04-29
      • 1970-01-01
      • 2021-08-28
      • 2020-10-16
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多