Question
Manipulating 2D arrays (matrices) in x86-64 assembly code In this question, you are asked to rotate a matrix 90 degrees clockwise. One way to do
Manipulating 2D arrays (matrices) in x86-64 assembly code
In this question, you are asked to rotate a matrix 90 degrees clockwise. One way to do this is to first transpose the matrix then to reverse its columns.
Your task is to implement these two functions in x86-64 assembly code:
void transpose(void *, int );
void reverseColumns(void *, int n);
Consider the following code:
.globl copy copy: # A in rdi, C in rsi, N in edx xorl %eax, %eax # set eax to 0 # since this function is a leaf function, no need to save caller-saved registers rcx and r8 xorl %ecx, %ecx # row number i is in ecx -> i = 0
# For each row rowLoop: movl $0, %r8d # column number j in r8d -> j = 0 cmpl %edx, %ecx # loop as long as i - N < 0 jge doneWithRows
# For each cell of this row colLoop: cmpl %edx, %r8d #loop as long as j - N < 0 jge doneWithCells
# Compute the address of current cell that is copied from A to C # since this function is a leaf function, no need to save caller-saved registers r10 and r11 movl %edx, %r10d # r10d = N imull %ecx, %r10d # r10d = i*N addl %r8d, %r10d # j + i*N imull $1, %r10d # r10 = L * (j + i*N) -> L is char (1Byte) movq %r10, %r11 # r11 = L * (j + i*N) addq %rdi, %r10 # r10 = A + L * (j + i*N) addq %rsi, %r11 # r11 = C + L * (j + i*N)
# Copy A[L * (j + i*N)] to C[L * (j + i*N)] movb (%r10), %r9b # temp = A[L * (j + i*N)] movb %r9b, (%r11) # C[L * (j + i*N)] = temp
incl %r8d # column number j++ (in r8d) jmp colLoop # go to next cell
# Go to next row doneWithCells: incl %ecx # row number i++ (in ecx) jmp rowLoop # Play it again, Sam!
doneWithRows: # bye! bye! ret
##################### .globl transpose transpose:
ret
##################### .globl reverseColumns reverseColumns:
ret
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started