如何将布尔数组(0或1个字节)转换为二进制整数? [英] How convert an array of bool (0 or 1 bytes) into a binary integer?
本文介绍了如何将布尔数组(0或1个字节)转换为二进制整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助如何使用二进制数组(在程序中填充数组的值)并为total变量分配值total = total * 2 + digit(数字是数组中的二进制数字),并在循环结束后输出值控制台中总变量的数量,是否在masm32中完成?
Please help how to go by binary array (values of array are filled in program) and assign the total variable the value total = total * 2 + digit (digit is binary digit from array) and after the loop finished, output value of variable total in the console and do it in the masm32?
array = [1, 1];
let total = 0;
for (let i = 0; i < length_of_array; i++){
total = total * 2 + array[i];
}
print(total); // 3
推荐答案
您可以编写以下代码:
.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
这可行,但是根据您运行的环境,您可能需要更改打印部分以其他方式运行.
This works, but depending on which environment you're running it, you might need to change the print section to run in a different manner.
这篇关于如何将布尔数组(0或1个字节)转换为二进制整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文