Question: Based on this C code, could you implement read _ record, sift _ up , sift _ down, heap _ insert and heap _ remove

Based on this C code, could you implement read_record, sift_up, sift_down, heap_insert and heap_remove in asembly code for Windows. Ive done heap_swap and heap_count already(included in images). Basically commenting out the definition in C code, leaving the prototype, and then implementing it in Assembly.
C:
/*
void heap_swap(int i, int j){
RECORD *temp = heap[i];
heap[i]= heap[j];
heap[j]= temp;
}
*/
Assembly:
.text
.globl heap_swap
heap_swap:
# Load the address of heap array into %rax
leaq heap(%rip),%rax # %rax = address of heap
# Load heap[i](using %rcx as i) into %r8
movq (%rax, %rcx,8),%r8 # %r8= heap[i]
# Load heap[j](using %rdx as j) into %r9
movq (%rax, %rdx,8),%r9 # %r9= heap[j]
# Swap heap[i] and heap[j]
movq %r9,(%rax, %rcx,8) # heap[i]= heap[j]
movq %r8,(%rax, %rdx,8) # heap[j]= heap[i]
ret ```
typedef struct {
char *last_name;
char *first_name;
long id_number;
} RECORD;
RECORD *read_record();
RECORD *read_record()
{
char lastname[100], firstname[100];
RECORD *p;
long id;
int res = scanf("%s %s %ld", lastname, firstname, 8id);
if (res == EOF)// EOF is -1
return NLLL;
p = malloc(sizeof(RECORD));
p->last_name = malloc(strlen(lastname)+1);
strcpy(p->last_name, lastname);
p->first_name = malloc(strlen(lastname)+1);
strcpy(p->first_name, firstname);
p->id_number = id;
return p;
##define PARENT(x)((x-1)>>1)
##define LEFT(x)((x1)+1)
##define RIGHT(x)((x1)+2)
##define SIZE 500
RECORD *heap[SIZE];
//int heap_count =8;
extern int heap_count; // in assembly
void heap_swap(int i, int j);
void heap_swap(int i, int j){
RECORD *temp = heap[i];
heap[i]= heap[j];
heap[j]= temp;
}
void sift_up(int i);
void sift_up(int i)
{
while (i !=0){
if (heap[i]->id_number >= heap[PARENT(i)]->id_number)// correct situation, no need to continue
return;
// otherwise, need to swap with parent
heap_swap(i, PARENT(i));
i = PARENT(i);
}
}
```
void sift_down();
void sift_domn()
{
int i =0;
int last_parent = PARENT(heap_count -1);
int min_child;
while (i = last_parent){
if ((RIGHT(i)>= heap_count)||
(heap[LEFT(i)]->id_number = heap[RIGHT(i)]->id_number))
min_child = LEFT(i);
else
min_child = RIGHT(i);
if (heap[i]->id_number > heap[min_child]->id_number){
heap_swap(i, min_child);
i = min_child;
}
else // done sifting down
return;
}
}
void heap_insert(RECORD *p);
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++;
}
RECORD *heap_remove();
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;
}
.text
.globl heap_swap
.globl heap_insert
.globl read_record
heap_swap:
Load the address of heap array into %rax
leaq heap(%rip),%rax # %rax = address of heap
Load heap[i](using %rcx as i) into %r8
movq (%rax, %rcx,8),%r8 # %r8= heap[i]
Load heap[j](using %rdx as j) into %r9
movq (%rax, %rdx,8),%r9 # %r9= heap[j]
Swap heap[i] and heap[j]
movq %r9,(%rax, %rcx,8) # heap[i]= heap[j]
movq %r8,(%rax, %rdx,8) # heap[j]= heap[i]
ret
.text
.globl heap_swap
.globl heap_insert
.globl read_record
heap_swap:
Load the address of heap array into %rax
leaq heap(%rip),%rax # Xrax = address of heap
Load heap[i](using %rcx as i) into %r8
movq (%rax, %rcx,8),%r8 # %r8= heap[i]
Load heap[j](using %rdx as j) into %r9
movq (%rax, %rdx,8),%r9 # %r9= heap[j]
Swap heap[i] and heap[j]
movq %r9,(%rax, %rcx,8) # heap[i]= heap[j]
movq %r8,(%rax, %rdx,8) # heap[j]= heap[i]
ret
.data
.globl heap_count
heap_count:
.long 0 # Initialize heap_count to 0
Based on this C code, could you implement read _

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!