【问题标题】:How convert an array of bool (0 or 1 bytes) into a binary integer?如何将 bool 数组(0 或 1 字节)转换为二进制整数?
【发布时间】:2021-08-05 22:10:26
【问题描述】:

请帮助如何通过二进制数组(数组的值在程序中填充)并分配总变量值total = total * 2 + digit(数字是来自数组的二进制数字),循环完成后,输出值控制台中的变量总数并在 masm32 中执行?

array = [1, 1];
let total = 0;
for (let i = 0; i < length_of_array; i++){
 
total = total * 2 + array[i];
 
}
 
print(total); // 3

【问题讨论】:

标签: assembly x86 masm masm32


【解决方案1】:

您可以编写以下代码:

.386
.model flat, stdcall
.stack 1000h

include \masm32\include\masm32rt.inc

.data
array db 1, 1 ; defining the array
arrlength db 2 ; array length

consoleOutHandle dd ? 
bytesWritten dd ? 
message db "------",13,10
lmessage dd 8 ; print declarations

.code
main proc

    mov ebx, offset array ; array location
    mov eax, 0 ; total
    mov ecx, 0 ; i
    loopLabel:
        push ecx
        mov ecx, 2
        mov edx, 0
        mul ecx ; eax = eax * 2
        pop ecx
        add ebx, ecx ; ebx is pointing to array[i]
        add al, byte ptr [ebx]
        sub ebx, ecx ; resetting ebx
        inc ecx
        cmp cl, byte ptr [arrlength] ; checking if i is bigger or equal to the length
    jb loopLabel
    ; the print section
    invoke dwtoa, eax, offset message
    INVOKE GetStdHandle, STD_OUTPUT_HANDLE
    mov consoleOutHandle, eax 
    mov edx,offset message 
    pushad    
    mov eax, offset message
    INVOKE WriteConsoleA, consoleOutHandle, edx, eax, offset bytesWritten, 0
    popad

    invoke ExitProcess, 0
main endp

end main
; 00F9FC60

这可行,但根据您运行它的环境,您可能需要更改打印部分以以不同方式运行。

【讨论】:

  • 这是正确的(我认为),但效率很低,对于未来的读者来说不是一个好例子。您不需要扩大 mul 以乘以 2,尤其是当您不需要高半部分时。只需add eax, eaxshl eax, 1。此外,mov edx, 0mul 覆盖 EDX 之前是没有意义的。如果你真的想使用mul edx,你可以使用mov edx, 2,但即使imul eax, 2也不会比mul差。
  • 您也不需要添加/子指针和计数器;要么使用[ebx + ecx],要么直接增加指针。例如inc ebx/dec ecx / jnz loopLabel。 (在循环之外,movzx ecx, byte ptr [arrlength] 如果你想将数组长度存储在内存中,而不是mov ecx, lengthof array;我认为这是正确的 MASM 运算符)。
  • 针对这个问题,优化total = total*2 + digit的另一个技巧是根据数字设置CF(例如cmp zeroed_reg, byte ptr [ebx]),然后adc eax,eax将CF值移入底部登记册。或者当然,如果您有 MMX 或 SSE2,SIMD 一次转换多个布尔值会更有效。
  • @Shufi123,它给出了找不到 masm32rt.inc 的错误,但是在我添加 C:/ 之后,错误消失了,并且它给出了新的错误,例如 windows.inc not found 我在 masm32rt.inc C 中添加:/ 到路径,它给出了类似的错误,在我再次将 C:/ 添加到路径后,它现在给出警告,例如: LINK : 警告 L4051: \masm32\lib\masm32.lib : 找不到库 LINK : 警告 L4051: \masm32\lib\gdi32.lib : 找不到库我需要做什么来解决这一切?
  • 即使你想将寄存器设置为零然后use xor reg, reg instead of mov reg, 0
猜你喜欢
  • 2017-04-25
  • 2013-09-09
  • 2015-01-30
  • 2013-01-09
  • 2019-03-29
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
相关资源
最近更新 更多