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

{Convert this to Y86 please} bubbleSort: pushl %ebp movl %esp, %ebp pushl %edi p

ID: 3722374 • Letter: #

Question

{Convert this to Y86 please}

bubbleSort:

pushl %ebp

movl %esp, %ebp

pushl %edi

pushl %esi

pushl %ebx

movl 8(%ebp), %edi

movl 12(%ebp), %eax

leal -1(%eax), %esi

testl %esi, %esi

jle .L1

leal -4(%edi,%eax,4), %ebx

jmp .L3

.L5:

movl (%eax), %ecx

movl 4(%eax), %edx

cmpl %edx, %ecx

jle .L4

movl %ecx, 4(%eax)

movl %edx, (%eax)

.L4:

addl $4, %eax

cmpl %ebx, %eax

jne .L5

.L6:

subl $4, %ebx

subl $1, %esi

je .L1

.L3:

testl %esi, %esi

jle .L6

movl %edi, %eax

jmp .L5

.L1:

popl %ebx

popl %esi

popl %edi

popl %ebp

ret  

.size bubbleSort, .-bubbleSort

.globl main

.type main, @function

main:

pushl %ebp

movl %esp, %ebp

pushl $10

pushl $array

call bubbleSort

addl $8, %esp

leave

ret

.size main, .-main

.globl array

.data

.align 32

.type array, @object

.size array, 40

array:

.long 10

.long 9

.long 8

.long 7

.long 6

.long 5

.long 4

.long 3

.long 2

.long 1

Explanation / Answer

Please find the complete working of Y86 Bubble Code:-

# Execution begins at address 0

.pos 0

init: irmovl Stack, %esp # Setup stack pointer

irmovl Stack, %ebp # Set up base pointer

call Main # Execute main program

halt # Terminate program

# Array of 10 elements

.align 10

array: .long 0x3

.long 0x8

.long 0x7

.long 0x2

.long 0x9

.long 0x5

.long 0x4

.long 0x1

.long 0x6

.long 0x0

#Main function, calls bubble and passes an array and the number of elements

Main: pushl %ebp

rrmovl %esp,%ebp

irmovl $10, %eax

pushl %eax # Push 10

irmovl array,%edx

pushl %edx # Push array

call bubble # bubble(array, 10)

rrmovl %ebp,%esp

popl %ebp

ret

bubble:

pushl %ebp

rrmovl %esp,%ebp

pushl %ebx

mrmovl 8(%ebp),%ecx # ecx = array

mrmovl 12(%ebp),%edx # edx = Size(10)

andl %edx, %edx # Set condition codes

je End

#Outer Loop

Loop:

mrmovl 8(%ebp),%ecx # ecx = array

mrmovl (%ecx), %esi # j in the array

mrmovl 4(%ecx), %edi # j + 1 in the array

irmovl $-1, %ebx

xorl %eax,%eax # j = 0

addl %ebx,%edx # edx Size--

jne Loopl # if it does not equal 0 jump to Loopl

jmp Loop

#Inner Loop

Loopl:

#for loop checks condition

irmovl $1,%ebx

addl %ebx, %eax #j++

#if statement

rrmovl %esi, %ebx #temporary

subl %edi, %ebx #subtracts j+1 - j

jle Shift #equals less than or equal to 0

rrmovl %esi, %ebx #move to temporary

rmmovl %edi, (%ecx)

rmmovl %ebx, 4(%ecx)

jmp Shift

#Moves ecx so it is not stuck comparing the first two elements

Shift:

#checks if i is equal to 0

rrmovl %edx, %ebx

subl %eax, %ebx #i - j

je Loop

irmovl $4,%ebx

addl %ebx,%ecx

mrmovl (%ecx), %esi #takes care of the swapping

mrmovl 4(%ecx), %edi

jmp Loopl

End:

popl %ebx

rrmovl %ebp,%esp

popl %ebp

ret

# The stack starts here and grows to lower addresses

.pos 0x100

Stack:

Please let me know in case of any clarifications required. Thanks!