1

我需要在循环中从 0 计数到 18446744073709551616(2^64)。

我可以使用 xmm 注册机吗?

例如这样的事情

 MOV XMM0,0
 LABEL1:
 INC XMM0
 CMP XMM0,18446744073709551616;(2^64)=18446744073709551616
 JNE LABEL1

如果没有,我该怎么办?

4

3 回答 3

3

我的程序集相当生锈,但是您可以在adc(带进位的加法)的帮助下获得 96 位(32 * 3)计数,如下所示:

; Reset our "composite register" ecx:ebx:eax to 0:
  xor eax, eax
  xor ebx, ebx
  xor ecx, ecx

loop:
  add eax, 1
  adc ebx, 0 ; adds 1 to ebx if eax just overflowed, 0 otherwise
  adc ecx, 0 ; adds 1 to ecx if the previous addition overflowed

  cmp ecx, 1 ; This is true after 2^64 iterations
  jne loop
于 2013-08-29T19:20:16.717 回答
1

您不需要 128 位寄存器。增加一个 64 位寄存器并检查溢出。

       xor rax, rax
again: inc rax
       jz again ; zero flag is set on inc overflow

...或者你可以用循环来做(但循环指令可能“慢”)

       xor rcx, rcx
again: loop again ; ~rcx counts UP to 2^64 as rcx counts down to 0

... add 使用比 inc 更多的操作码空间,但可能更快

        xor rax, rax
 again: add rax, 1
        jc again

...展开循环以获得更快的速度

        xor rax, rax
 again: add rax, 1
        add rax, 1
        add rax, 1
        add rax, 1
        add rax, 1
        add rax, 1
        add rax, 1
        add rax, 1
        jc again ; only test needed as we know final_value % 8 == 0

此外,cpu 的每个核心始终在执行您从启动时请求的功能。您可以使用 RDTSC 指令轮询其进度。

 rdtsc ; progress reported in rdx:rax 

每个人都想知道你的问题的重点。我假设有一个,因为你问过它。对于那些无法考虑原因的人:

  a) progress will be inspected before completion
  b) fool unreachable code "error" detection
  c) wasQuantumComputerInvented() function
  d) easier to ask than "real" question
  e) OP didn't originally realize how long it might take
  f) or simply a learning exercise 

...但是说真的,有什么意义呢?

于 2017-01-18T23:57:46.277 回答
0
loop:
  JMP loop

大约 400 年后,这个循环将达到 2^64。

任何人在没有达到 2^64 的情况下运行了这么长时间,请告诉我,但没有关于该算法失败的报告,因为无论出于何种意图和目的,它与公认的答案一样有用。

于 2015-04-28T08:00:48.757 回答