USING x86 ASSEMBLY LANGUAGE: PART 1: Given a byte (x), swap every even bit with
ID: 3882207 • Letter: U
Question
USING x86 ASSEMBLY LANGUAGE:
PART 1: Given a byte (x), swap every even bit with its adjacent odd bit. For example 100 in decima!l is represented as 81108180 in a byte (or 8 bits). If we swap the even bits with their adjacent odd bits, we get 18811000, which is 152 in decimal. Implementation details: The input integer is stored in registers cl. You need to store the answer into register al. unsigned char swapBits(unsigned char x) unsigned char result; asm xor al, al mov cl,x /YOUR CODE STARTS HERE / YOUR CODE ENDS HERE mov result, al return result;Explanation / Answer
Part 1:
Please find below code, and let me know in case of any error/doubt in the comment.
code:
__asm
{
xor al, al
mov cl, x
and cl, 0x55555555
shl cl, 1
mov al, x
and al, 0xAAAAAAAA
shr al, 1
or al, cl
mov result, al
}
Part 2:
Please check below code and let me know in case of any error or doubt in the comment:
section .text
global _start
section .data:
global result
result:
db 0
_start:
xor eax, eax
mov esi, result
mov ebx, a
mov ecx, b
test ebx, 3
jnz not_multiple_of_4
and ebx, 0x0F
shl ebx, 4
and a, 0xF0
shr a, 4
or ebx, a
add ebx, b
mov eax, ebx
_not_multiple_of_4:
; ...
and ecx, 0x0F
shl ecx, 4
and b, 0xF0
shr b, 4
or ecx, b
add ecx, a
mov eax, ecx
mov[esi][0], eax
retn
section .bss:
mov eax, 1
int 0x80
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.