Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please assist with this x86-64 assembly language code. Now, your job in life is

ID: 3601029 • Letter: P

Question

Please assist with this x86-64 assembly language code.

Now, your job in life is to write the function “rotate” in x86-64 assembly language.

Remember that the prototypes for this function looks like this: void rotate( char t[][ 4 ] );

When you write the “rotate” function you may want to use a local variable that is an array 4x4 of characters. We know how to do this by allocating 16 bytes on the stack, just like we have talked about in class. (Need help especially with this part.)

We are passing a 4x4 array to the Rotate function. From there, we need to rotate all of the values clockwise. This needs to be in assembly.

Here's the starter code:

   .globl   rotate
   .type   rotate, @function
rotate:
   push   rbp
   mov   rbp, rsp
   mov   QWORD PTR [rbp-8], rdi
   # Here's where you put the code for rotate.
   pop   rbp
   ret

   .section   .note.GNU-stack,"",@progbits

Explanation / Answer

Suppose that we have a 32-bit DWORD variable:

Hide
.data
var1 DWORD 123
The example is to add var1 to EAX. This is correct with MOV and ADD:

Hide
mov ebx, var1
add eax, ebx
But as ADD can accept one memory operand, you can just

Hide
add eax, var1

2. Using an instruction with less bytes

Suppose that we have an array:

Hide
.data
array DWORD 1,2,3
If want to rearrange the values to be 3,1,2, you could

Hide
mov eax,array ; eax =1
xchg eax,[array+4] ; 1,1,3, eax =2
xchg eax,[array+8] ; 1,1,2, eax =3
xchg array,eax ; 3,1,2, eax =1
But notice that the last instruction should be MOV instead of XCHG. Although both can assign 3 in EAX to the first array element, the other way
around in exchange XCHG is logically unnecessary.

aware of code size, MOV takes 5-byte machine code but XCHG takes 6, as another reason to choose MOV here:

Hide Copy Code
00000011 87 05 00000000 R xchg array,eax
00000017 A3 00000000 R mov array,eax