simpley convert the code to assembly language Your proof-of-study task is to han
ID: 3753199 • Letter: S
Question
simpley convert the code to assembly language
Your proof-of-study task is to hand-compile the code below into an assembly language function (a procedure), then place it in the provided .asm file, test it, and turn it in. Your function must adhere to the procedure calling conventions covered in class. You must also use specific registers when storing local variables, see below.
Before you start: You may use my tests -- I've set them up to make it easy for you to test your code, and I have examples in the tests! :) Here they are:
Proof-of-Study #4.asm
Here is a recursive sorting algorithm that you will convert into assembly:
Note that the code looks similar to Java, C, or many other languages. It has variables (assumed to be integers or arrays), a loop, and 'if' statements. The loop and 'if' statements use indentation to show scope.
When converting this algorithm, use only these registers for variables:
When needed, you can use $t4-$t9 for additional temporary computations (addresses, etc.), but not for program variables. You may use pseudoinstructions.
You will need to add code to the function to properly create a stack frame and to preserve registers appropriately. Follow the convention covered in class (required). Clearly comment your code. You may write the assembly code for your function separately, or you may write it as part of this test program (easier):
Do not optimize your program. Each C/Java statement should be independently translated to assembly. Don't avoid writing code because you want to reuse a previous temporary value - always write the complete code for each statement. (You can verify correctness much faster by doing it this way.)
After you convert the statements, add the code to create the stack frame and do preservation of registers.
The code is recursive. Be careful to preserve the needed temporary variable(s) just before you make the first function call, and restore it/them when the function call completes.
If you're curious, note that I adapted a quicksort algorithm to be easy to code in assembly.
Do not add sorting code (or other critical statements) to 'main' because it won't be in our version of main. You can change your main as needed for testing (but remember that we will not use your 'main' function).
Your sort procedure should be contiguous statements. Don't embed other functions within your sort function. We'll only copy your sort function into our test code, and it needs to be easy for us to extract it.
Variable Register Notes data $s1 Array address, it arrives in $a0 but your code will copy $a0 to $s1 and use $s1 instead lowIndex $s2 integer, it arrives in $a1 but your code will copy $a1 to $s2 and use $s2 instead highIndex $s3 integer, it arrives in $a2 but your code will copy $a2 to $s3 and use $s3 instead tempValue $t0 integer tempIndex $t1 integer centerValue $t2 integer centerIndex $t3 integerExplanation / Answer
.LC0:
.string "Entering sort: "
.LC1:
.string "the value"
.LC2:
.string "Leaving sort: "
sort:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-32], edx
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-28]
mov edx, eax
mov esi, OFFSET FLAT:.LC1
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov edi, 32
call putchar
mov eax, DWORD PTR [rbp-32]
cdqe
mov rdi, rax
mov eax, 0
call printf
mov edi, 10
call putchar
mov eax, DWORD PTR [rbp-28]
cmp eax, DWORD PTR [rbp-32]
jl .L2
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-28]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov edi, 32
call putchar
mov eax, DWORD PTR [rbp-32]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov edi, 10
call putchar
.L2:
mov eax, DWORD PTR [rbp-28]
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-28]
mov DWORD PTR [rbp-8], eax
mov eax, DWORD PTR [rbp-32]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-12], eax
jmp .L3
.L4:
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-16], eax
mov eax, DWORD PTR [rbp-16]
cmp eax, DWORD PTR [rbp-12]
jge .L3
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rbp-4]
movsx rdx, edx
lea rcx, [0+rdx*4]
mov rdx, QWORD PTR [rbp-24]
add rdx, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rdx, rax
mov eax, DWORD PTR [rbp-16]
mov DWORD PTR [rdx], eax
add DWORD PTR [rbp-8], 1
add DWORD PTR [rbp-4], 1
.L3:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-32]
jl .L4
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rbp-32]
movsx rdx, edx
lea rcx, [0+rdx*4]
mov rdx, QWORD PTR [rbp-24]
add rdx, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rdx, rax
mov eax, DWORD PTR [rbp-12]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-8]
lea ecx, [rax+1]
mov edx, DWORD PTR [rbp-32]
mov rax, QWORD PTR [rbp-24]
mov esi, ecx
mov rdi, rax
call sort
mov eax, DWORD PTR [rbp-8]
lea edx, [rax-1]
mov ecx, DWORD PTR [rbp-28]
mov rax, QWORD PTR [rbp-24]
mov esi, ecx
mov rdi, rax
call sort
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-28]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov edi, 32
call putchar
mov eax, DWORD PTR [rbp-32]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov edi, 10
call putchar
nop
leave
ret
main:
push rbp
mov rbp, rsp
sub rsp, 4016
mov edx, DWORD PTR [rbp-8]
mov ecx, DWORD PTR [rbp-4]
lea rax, [rbp-4016]
mov esi, ecx
mov rdi, rax
call sort
mov eax, 0
leave
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.