2014-05-17 3 views
0

sasm ide를 사용하고 있습니다. 내 질문은 어떻게 플로트 번호를 인쇄하는 것입니다. 이 프로그램은 성공적으로 실행되었지만 아무 번호도 표시하지 않았습니다. 내 코드는 다음과 같습니다.SASM에서 플로트 번호를 인쇄하는 방법은 무엇입니까?

%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 
+0

를 닮아 코드 : http://en.wikibooks.org/wiki/X86_Assembly/Floating_Point 네,하지만 부동 소수점 숫자를 displaay하는 방법이 표시되지 않았습니다 – SSpoke

+0

플로이드 번호를 표시하는 방법을 알려주세요. – Junaid

+0

어떤 OS를 타겟팅하는지 전혀 알 수 없습니다. – Michael

답변

0
%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 
+0

이것은 간단합니다 – Junaid

1

FIST으로 결과의 정수 부분을 분리 할 수 ​​있습니다. SASM에는 PRINT_FLOAT 매크로가 없으므로 결과의 소수 부분을 ASCIIZ 문자열로 변환해야합니다. 소수 부분에 10을 곱하여 숫자를 분리하여 관리 할 수 ​​있습니다. 결과 정수는 다음 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 
+0

이것은 어렵습니다 – Junaid

관련 문제