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

Write the LEGv8 assembly code for the following loop. This code compares two cha

ID: 3878697 • Letter: W

Question

Write the LEGv8 assembly code for the following loop. This code compares two character arrays and sets the variable different to 1 if they are not the same // A and B are arrays of characters Cunsigned, 8-bits per element). int different = 0; int index = 0; while(11) != B[index]){ if(A[index] break; if(A[index] different = 1; ''){ == break; index++; Assume different is in X9, the base address of A is in X10 and the base address of B is in X11; index should go in X12 Label Instruction Description Initialize the value of 'different Initialize the value of 'index

Explanation / Answer

Here is the completed code with comments, let me know if you have any doubts

LEGv8.asm

MOV X9, #0 ;initialise X9 (different) with 0
MOV X12, #0 ;initialise X12 (index) with 0

LOOP:
ADD X13, X10, X12 ;Add the index(X12) to X10(Base address of A) and store this in X13(A_i)
ADD X14, X11, X12 ;Add the index(X12) to X11(Base address of B) and store this in X14(B_i)
LDUR X13, [X13] ;Load the contents of X13 into the register X13
LDUR X14, [X14] ;Load the contents of X14 into the register X14
SUBS X15, X13, X14 ;Compute the difference of X13 and X14 while also setting flags
BEQ SECONDIF ;If the zero flag is set(A_i and B_i are equal ) branch to SECONDIF
MOV X9, #1 ;Set different to 1
B EXIT ;Branch to EXIT

SECONDIF:
CBZ X13, EXIT ;Compare if X13 contains 0, if it does, jump to EXIT
ADDI X12, X12, #1 ;Add 1 to X12
B LOOP ;Branch to LOOP

EXIT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote