【发布时间】:2017-02-13 10:26:39
【问题描述】:
我正在尝试为 LC3 模拟器编写一个程序,该程序将允许我计算存储在内存中其他位置的二进制数中 1 的数量。这是我目前所拥有的:
0011 0001 0000 0000 ; Start the data at memory location x3100
0110 1010 1111 0001 ; Hex number stored at x3000
0011 0000 0000 0000 ; Start the program at x3000
0101 001 001 1 00000 ; Clear R1 (Contain address of number)
0101 010 010 1 00000 ; Clear R2 (Counter for amount of 1's)
0001 011 011 1 00001 ; Load R3 with 1 (Number for 'and-ing' with number getting checked)
;^cant do this line since that is a decimal 1 not binary one therefore it wouldnt left shift
; and cant store and get a binary number in memory
0001 100 100 1 01111 ; Load R4 with 16 (Loop loops til 0)
1110 001 011111100 ; Load R1 with address of number
0110 101 001 000000 ; Load R5 with the number stored at x3100
0101 110 101 000 011 ; And R3 with R5 store result in R6
; If number is not zero, increment R2 by 1
0001 011 011 000 011 ; Add R3 with itself to make a left shift
0001 100 100 1 11111 ; Decrement R4 by 1
; Loop to x3006 (When R3 is 'And-ed' with R5) if R4 isnt 0
0011 010 011111101 ; Store value from R2 in x3101
1111 0000 00100101 ; Halt (Is this correct?) set breakpoint here
我很困惑如何制作“if 语句”来检查某些值以及如果不满足条件如何循环回到某个点。我对如何实际计算 1 的数量的思考过程是,如果结果值不是 0,则使用“0000000000000001”检查原始二进制数,然后将 1 添加到我的“1 计数器”,然后将 1 值左移到检查原始数字中的下一个值。我注意到我不能做某行,因为我相信我在寄存器 3 中存储了十进制 1 而不是二进制,并且不能左移它。我的限制之一是我不能使用内存中除 x3100 和 x3101 之外的任何其他位置来分别存储原始数字和 1 的数量。
我听说位掩码非常有用,但我搜索了高位和低位,却找不到如何使用它。任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: assembly binary hex bitmask lc3