如何在程序集x86中从0到18446744073709551616(2 ^ 64)计数?

时间:2013-08-29 18:46:05

标签: assembly x86 sse

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

我可以使用xmm regiters吗?

例如像这样的事情

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

如果没有,我该怎么办?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

...添加使用比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 

......但是说真的,重点是什么?

答案 2 :(得分:0)

loop:
  JMP loop

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

任何人如果长时间没有达到2 ^ 64那么运行,请告诉我,但是没有关于此算法失败的报告,因为,无论出于何种意图和目的,它都与接受的答案一样有用