Assignment: Use bit wise instructions
lab requirement: use bitwise instructions when possible to make the code shorter
INCLUDE Irvine32.inc
VAL = 129
.data
bvar BYTE 0fh
wvar WORD -17
dvar DWORD 13
outstr1 BYTE "Number of 1's: ", 0 use for output of question 1
power2 BYTE "Power of 2", 0ah, 0dh, 0 use for output of question 2
notPower2 BYTE "Not power of 2", 0ah, 0dh, 0 use for output of question 2
arr WORD 1, -2, -3, 4
.code
main PROC
Question 1:
Add code below to print how many 1's there are in a data value, without changing the data itself. Use outstr1 for your text output.
Your code should work with dvar, but if I change the variable name to wvar or bvar, the code should still run without any other modification.
Question 2a
Write code to determine if a data value is a power of 2, using the formula:
bool powerOf2 = val&& !(val& (val - 1))
where& is a bitwise AND, && is a logical AND, ! is a logical NOT (there is a difference between bitwise AND vs logical AND, don't mix them up same with bitwise NOT and logical NOT)
You *cannot* use the CMP instruction in your code (try a bitwise instruction)
Use power2 and notPower2 for your text output.
moveax, VAL Your code should work with all VAL values
Question 2b: explain in your own words how the formula above works
Question 3: Given an array arr as defined in .data, and ebx is initialized below.
Using ebx (not the array name), write ONE instruction to reverse the LSB of the 2nd and 3rd elements of arr. Reverse means 0 to 1 or 1 to 0. Your code should work with all values in arr (not just the sample values above).
movebx, OFFSET arr
exit
main ENDP
END main