Question: Can you help me figure out why heap _ remove does not output anything? The assembly code for heap _ insert is correct but heap

Can you help me figure out why heap_remove does not output anything? The assembly code for heap_insert is correct but heap_remove does not work. ```
RECORD *heap_remove()
{
if (heap_count =0){
printf("Error: Heap is empty
");
exit(1);
}
RECORD *result = heap[0];
heap_count--;
heap[0]= heap[heap_count];
sift_down();
return result;
}
``````
void heap_insert(RECORD *p)
{
if (heap_count >= SIZE){
printf("Error: Heap is full
");
exit(1);
}
heap[heap_count]= p;
sift_up(heap_count);
heap_count++;
}
``````
heap_insert:
pushq %rbp # Save base pointer
movq %rsp,%rbp # Set up stack frame
leaq heap(%rip),%rax # Load address of heap into %rax
movq heap_count(%rip),%r11 # Load heap_count into %rcx
# Check if heap_count >= SIZE
cmpq $500,%r11 # Compare heap_count with SIZE
jge .heap_full # If heap_count >= SIZE, jump to heap_full
# Insert the new element (passed in %rcx) at heap[heap_count]
movq %rcx,(%rax, %r11,8) # Store the record in heap[heap_count]
# Call sift_up to restore heap property
movq %r11,%rcx # Pass the index (heap_count) to sift_up
sub $32,%rsp
call sift_up
add $32,%rsp
# Increment heap_count
incq heap_count(%rip) # Increment heap_count
popq %rbp # Restore base pointer
ret # Return from function
.heap_full:
leaq error_msg_full(%rip),%rcx # Load error message address
call printf # Print error message
movq $1,%rcx # Set exit code
call exit # Exit the progr
``````
heap_remove:
pushq %rbp
movq %rsp,%rbp
movq heap_count(%rip),%rax
cmp $0,%rax
jle .error
movq heap(%rip),%rax #rax = heap[0]
movq (%rax),%rbx #rbx = rax
decq heap_count(%rip)
movq heap_count(%rip),%rcx
movq heap(%rip),%rax
movq (%rax, %rcx,8),%rdx #rdx = heap[heap_count]
movq %rdx,(%rax) #heap[0]= heap[heap_count]
call sift_down
movq %rbx,%rax
pop %rbp
ret
.error:
leaq error_msg_empty(%rip),%rcx
call printf
movq $1,%rcx
call exit
```
Can you help me figure out why heap _ remove does

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!