【问题标题】:How to print float number in SASM?如何在 SASM 中打印浮点数?
【发布时间】:2014-05-17 11:47:20
【问题描述】:

我正在使用 sasm ide。我的问题是如何打印浮点数。该程序执行成功,但没有显示任何数字。我的代码如下:

%include "io.inc"

section .data
val: dq a123.45

section .bss
res:resq 1
section .text
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    ;write your code here
   fld qword[val]
 fsqrt
   fst qword[res]


     xor eax, eax
    ret 

【问题讨论】:

  • 是的,但是如何显示浮点数,它没有显示任何东西,请告诉我如何显示浮点数
  • 我没有看到任何提及您的目标操作系统。
  • 请有人告诉我如何显示浮点数.......
  • 使用printf。我保证它会起作用。

标签: assembly nasm


【解决方案1】:

您可以使用FIST 隔离结果的整数部分。由于 SASM 没有 PRINT_FLOAT 宏,因此您已将结果的小数部分转换为 ASCIIZ 字符串。这可以通过将小数部分乘以 10 来隔离数字来管理。得到的整数是下一个十进制数字。注意舍入方式,因为FIST会根据当前的舍入方式对整数进行四舍五入!随后的FISUB 不得产生负数。

我的建议:

%include "io.inc"

section .data
    val: dq 123.45
    ten: dd 10

section .bss
    leftdigits: resd 1
    rightdigits: resb 100
    temp: resd 1
    control_word: resw 1

section .text
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    ;write your code here
    fld qword[val]
    fsqrt

    ; modifying rounding mode
    fstcw [control_word]
    mov ax, [control_word]
    or ah, 0b00001100               ; rounding mode: truncating
    mov [temp], ax
    fldcw [temp]                    ; load new rounding mode

    fist dword [leftdigits]         ; store integer part
    fisub dword [leftdigits]        ; clear integer part

    ; load 10 and move it to st(1)
    fild dword [ten]
    fxch

    ; isolate digits of fractional part and store ASCII
    mov edi, rightdigits            ; pointer to ASCIIZ-buffer
    .get_fractional:
    fmul st0, st1                   ; one decimal digit into integer
    fist dword [temp]               ; store digit
    fisub dword [temp]              ; clear integer part
    mov al, byte [temp]             ; load digit
    or al, 0x30                     ; to ASCII
    mov byte [edi], al              ; store to 'rightdigits'
    add edi, 1                      ; increment pointer to string
    fxam                            ; st0 == 0.0?
    fstsw ax
    fwait
    sahf
    jnz .get_fractional             ; no: once more
    mov byte [edi], 0               ; null-termination for ASCIIZ

    ; clean up FPU
    ffree st0                       ; empty st(0)
    ffree st1                       ; empty st(1)
    fldcw [control_word]            ; restore old rounding mode

    PRINT_DEC 4, leftdigits
    PRINT_CHAR '.'                  ; decimal point
    PRINT_STRING rightdigits

    xor eax, eax
    ret

【讨论】:

    【解决方案2】:
    %include "io.inc"
    
    
    section .data
    msg: db "Printing float number %f",0 ;format for print string
    val: dq 2.45 ;64 bit floating point
    
    section .bss
    res: resq 1
    section .text
    
    global CMAIN
    CMAIN:
    
        mov ebp, esp; for correct debugging
        ;write your code here
     fld qword[val]      ;need to convert 32 bit to 64 bit
       fstp qword[res]   ;floating load makes 80 bit
                         ;store as 64 bit
                         ;push last argument first  
      push dword[val+4] ;64 bit floating point (bottom)
       push dword[val]   ;64 bit floating point (top) 
       push dword[res+4] ;64 bit floating point (bottom)
       push dword[res]   ;64 bit floating point (top)
       push dword msg          ;adress of format string
      call printf
      add esp,20     ;pop stack
      mov eax,0          ; exit code, 0=normal
             xor eax, eax
        ret
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2020-08-04
      相关资源
      最近更新 更多