草庐IT

带 float 的 Windows ASM printf

coder 2024-06-09 原文

我一直试图在汇编程序中与 Windows 中的标准 C 库进行交互,但遇到了麻烦。出于某种原因,我无法让 printf 接受浮点变量,所以这里出了点问题。

这是我能创建的最短的演示问题的程序。我已经包含了解释我对应该发生的事情的理解的评论。

谢谢

;
; Hello64.asm
; A simple program to print a floating point number in windows
;
; assemble: nasm float64.asm -f win64
; link: golink /console /entry main float64.obj MSVCRT.dll
;

; tell assembler to generate 64-bit code
;
bits 64

; data segment
section .data use64

pi  dq 3.14159

textformat: db "hello, %lf!",0x0a, 0x00     ; friendly greeting

; set up the .text segment for the code
section .text use64

; global main is the entry point
global main
; note that there is no _ before printf here, unlike in OS X
extern printf

main:
mov rcx, textformat 
movq xmm0, qword [pi]
mov rax, 1      ; need to tell printf how many floats
call printf

; note next step - this puts a zero in rax
xor rax,rax
ret ; this returns to the OS based on how Windows calls programs.
; this return causes a delay then the program exits.

最佳答案

您设法混合了 Microsoft 和 sysv 约定。正确的做法是:

mov rcx, textformat 
movq xmm1, qword [pi]
movq rdx, xmm1  ; duplicate into the integer register
sub rsp, 40     ; allocate shadow space and alignment (32+8)
call printf
add rsp, 40     ; restore stack
xor eax, eax
ret

根据 MSDN ,当使用可变参数时:

For floating-point values only, both the integer and the floating-point register will contain the float value in case the callee expects the value in the integer registers.

关于带 float 的 Windows ASM printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943615/

有关带 float 的 Windows ASM printf的更多相关文章

  1. ruby-on-rails - 32651 :ERROR comparison of Float with Float failed ruby - 2

    我是Rails的新手,我遇到了一个错误,但我似乎找不到问题所在。这是日志:[32651:ERROR]2012-10-0913:46:52::comparisonofFloatwithFloatfailed[32651:ERROR]2012-10-0913:46:52::/home/sunny/backend/lib/analytics/lifetime.rb:45:in`each'/home/sunny/backend/lib/analytics/lifetime.rb:45:in`max'/home/sunny/backend/lib/analytics/lifetime.rb:45

  2. ruby - 为什么在 Ruby 中是 Float::INFINITY == Float::INFINITY? - 2

    在数学中,2个无穷大既不等于,也不大于或小于then。那么是什么给了?在irb中,Float::INFINITY==Float::INFINITY(在ruby​​1.9.3中测试) 最佳答案 用更专业的术语来说,这一切都归结为浮点运算的IEEE754标准。TheIEEE754standarddoesimplicitlydefineInfinity==Infinitytobetrue.Therelevantpartofthestandardissection5.7:"Fourmutuallyexclusiverelationsarep

  3. ruby - 强制 Ruby 不以标准形式/科学记数法/指数记数法输出 float - 2

    我遇到了同样的问题here对于python,但对于ruby​​。我需要输出这样一个小数字:0.00001,而不是1e-5。有关我的特定问题的更多信息,我正在使用f.write("Mynumber:"+small_number.to_s+"\n")输出到一个文件对于我的问题,准确性不是什么大问题,所以只做一个if语句来检查是否small_number那么更通用的方法是什么? 最佳答案 f.printf"Mynumber:%.5f\n",small_number您可以将.5(小数点右侧5位数字)替换为您喜欢的任何特定格式大小,例如,%8

  4. ruby - 使用 Ruby 将输入值适本地转换为整数或 float - 2

    我相信我对这个问题有一个很好的答案,但我想确保ruby​​-philes没有更好的方法来做到这一点。基本上,给定一个输入字符串,我想在适当的情况下将该字符串转换为整数,或在适当的情况下将其转换为float。否则,只返回字符串。我会在下面发布我的答案,但我想知道是否有更好的方法。例如:to_f_or_i_or_s("0523.49")#=>523.49to_f_or_i_or_s("0000029")#=>29to_f_or_i_or_s("kittens")#=>"kittens" 最佳答案 我会尽可能避免在Ruby中使用正则表达式

  5. ruby - 比较 rspec 中的 float 时的奇怪行为 - 2

    以下测试中的第3个失败:specify{(0.6*2).shouldeql(1.2)}specify{(0.3*3).shouldeql(0.3*3)}specify{(0.3*3).shouldeql(0.9)}#thisonefails这是为什么呢?这是浮点问题还是ruby​​或rspec问题? 最佳答案 从rspec-2.1开始specify{(0.6*2).shouldbe_within(0.01).of(1.2)}在那之前:specify{(0.6*2).shouldbe_close(1.2,0.01)}

  6. ruby - 猴子修补 float 中缀运算符产生意想不到的结果 - 2

    重新定义Float#/似乎没有效果:classFloatdef/(other)"magic!"endendputs10.0/2.0#=>5.0但是当另一个中缀运算符Float#*被重新定义时,Float#/突然采用了新的定义:classFloatdef/(other)"magic!"enddef*(other)"spooky"endendputs10.0/2.0#=>"magic!"我很想知道是否有人可以解释这种行为的来源,以及其他人是否得到相同的结果。ruby:ruby2.0.0p353(2013-11-22)[x64-mingw32]要快速确认错误,请运行thisscript.

  7. ruby - 如何检查一个值是否是 Integer()、Float() 或 Rational() 的有效输入? - 2

    这大致基于“HowtoconvertaStringtoIntegerorFloat”。如果我想使用Ruby的内置转换机制将数字字符串输入转换为其“最合适的类型”,我可以这样做:defconvert(input)value=Integer(input)rescuenilvalue||=Float(input)rescuenilvalue||=Rational(input)rescuenilvalueendconvert('1')#=>1convert('1_000')#=>1000convert('0xff')#=>255convert('0.5')#=>0.5convert('1e2'

  8. ruby - 如何强制 Float 在不使用科学记数法的情况下以完全精确的方式显示,而不是作为字符串显示? - 2

    在Ruby中,如何在没有科学记数法的情况下强制显示所有重要位置/完全精确的float?目前我将BigDecimal转换为Float,BigDecimal(0.000000001453).to_f,但这会产生1.453e-09的结果float。如果我执行类似"%14.12f"%BigDecimal("0.000000001453").to_f的操作,我会得到一个字符串。然而,在这种情况下,字符串作为输出是NotAcceptable,因为我需要它作为没有科学记数法的实际数字float。---编辑---好吧,让我在这里提供一些背景信息,这可能需要更改我原来的问题。我正在尝试使用Highsto

  9. ruby - 从 float 中删除科学记数法 - 2

    我目前正在将两个float相乘:0.0004*0.0000000000012=4.8e-16如何获得正常格式的结果,即没有科学记数法,例如0.0000000000324,然后将其四舍五入为5个数字。 最佳答案 您可以使用stringformatting.a=0.0004*0.0000000000012#=>4.8e-16'%.5f'%a#=>"0.00000"pi=Math::PI#=>3.141592653589793'%.5f'%pi#=>"3.14159" 关于ruby-从floa

  10. ruby - 如何将 float 格式化为一定数量的小数和整数? - 2

    我正在尝试将Ruby中的float格式化为四位数字,包括小数点。例如:1=>01.002.4=>02.401.4455=>01.45现在,我正在尝试按如下方式格式化float:str_result="%.2f"%result这成功地将小数位数限制为两位。我还知道:str_result="%2d"%result它成功地将1转换为01,但丢失了小数位。我试着像这样组合这些:str_result="%2.2f"%result没有明显效果。它与%.2f具有相同的结果。有没有办法强制Ruby将字符串格式化为这种四位数格式? 最佳答案 您可以使

随机推荐