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 heapremove does not output anything? The assembly code for heapinsert is correct but heapremove does not work.
RECORD heapremove
if heapcount
printfError: Heap is empty
;
exit;
RECORD result heap;
heapcount;
heap heapheapcount;
siftdown;
return result;
void heapinsertRECORD p
if heapcount SIZE
printfError: Heap is full
;
exit;
heapheapcount p;
siftupheapcount;
heapcount;
heapinsert:
pushq rbp # Save base pointer
movq rsprbp # Set up stack frame
leaq heapriprax # Load address of heap into rax
movq heapcountripr # Load heapcount into rcx
# Check if heapcount SIZE
cmpq $r # Compare heapcount with SIZE
jge heapfull # If heapcount SIZE, jump to heapfull
# Insert the new element passed in rcx at heapheapcount
movq rcxrax, r # Store the record in heapheapcount
# Call siftup to restore heap property
movq rrcx # Pass the index heapcount to siftup
sub $rsp
call siftup
add $rsp
# Increment heapcount
incq heapcountrip # Increment heapcount
popq rbp # Restore base pointer
ret # Return from function
heapfull:
leaq errormsgfullriprcx # Load error message address
call printf # Print error message
movq $rcx # Set exit code
call exit # Exit the progr
heapremove:
pushq rbp
movq rsprbp
movq heapcountriprax
cmp $rax
jle error
movq heapriprax #rax heap
movq raxrbx #rbx rax
decq heapcountrip
movq heapcountriprcx
movq heapriprax
movq rax, rcxrdx #rdx heapheapcount
movq rdxrax #heap heapheapcount
call siftdown
movq rbxrax
pop rbp
ret
error:
leaq errormsgemptyriprcx
call printf
movq $rcx
call exit
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
