assembly - 如何将 bool 数组(0 或 1 字节)转换为二进制整数?
问题描述
请帮助如何通过二进制数组(数组的值在程序中填写)并分配总变量值total = total * 2 + digit(数字是数组中的二进制数字),循环完成后,变量total的输出值在控制台中并在 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
这可行,但根据您运行它的环境,您可能需要更改打印部分以以不同的方式运行。
推荐阅读
- python - 对数组执行 argsort 操作时出现 IndexError
- javascript - 切换到 Preact 时,我的 React 库中的 JSX 出错
- javascript - 将迭代链接文本与另一个列表链接元素进行比较
- reactjs - 类型错误:类型“{}”中缺少属性“存储”,但类型“只读”中是必需的
'。TS2741 - c# - 带有 IN 子句的 WHERE - C#
- excel - 输入数据时生成日期戳
- java - 而循环+睡眠减慢PC
- powershell - Powershell不在变量中存储foreach循环信息
- python - 在 Python 2.7 中将 C 级流打印保存到文件
- reactjs - MUI:如何在选定文本上显示工具提示组件?