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

1. (a) Write Pep/ subroutine COMPAREV specified as follows: (i) It takes 3 param

ID: 3761213 • Letter: 1

Question

1. (a) Write Pep/ subroutine COMPAREV specified as follows:

(i) It takes 3 parameters – pointers to integer arrays V1 and V2 and an integer N

(ii) It returns 2 integer values on the stack

(iii) It examines elements 0..N-1 of V1 and V2

(iv) The two values returned are (1) a count of the number of times V1[i]>V2[i] and (2) a count of the number of times V2[i]>V1[i].

(b) Suppose a Pep/8 program contains

X1: .block 50

X2: .block 50

COUNT1: .block 2

COUNT2: .block 2

Write code that calls your COMPAREV subroutine from (a) with X1 and X2 as parameters then assigns the 2 results to COUNT1 and COUNT2.

Explanation / Answer

pep assembly


   .Ltext0:
                    .comm   COUNT1,4,4
                    .comm   COUNT2,4,4
                    .section   .rodata
                .LC0:
0000 0A20636F        .string   " count1 = %d count 2 = %d "
     756E7431
     203D2025
     64202063
     6F756E74
                    .text
                    .globl   COMPAREV
                COMPAREV:
                .LFB0:
                    .cfi_startproc
0000 55              pushq   %rbp
                    .cfi_def_cfa_offset 16
                    .cfi_offset 6, -16
0001 4889E5         movq   %rsp, %rbp
                    .cfi_def_cfa_register 6
0004 4883EC30        subq   $48, %rsp
0008 48897DE8        movq   %rdi, -24(%rbp)
000c 488975E0        movq   %rsi, -32(%rbp)
0010 8955DC         movl   %edx, -36(%rbp)
0013 C745FC00        movl   $0, -4(%rbp)
     000000
001a E9820000        jmp   .L2
     00
                .L5:
001f 8B45FC         movl   -4(%rbp), %eax
0022 4898            cltq
0024 488D1485        leaq   0(,%rax,4), %rdx
     00000000
002c 488B45E8        movq   -24(%rbp), %rax
0030 4801D0         addq   %rdx, %rax
0033 8B10            movl   (%rax), %edx
0035 8B45FC         movl   -4(%rbp), %eax
0038 4898            cltq
003a 488D0C85        leaq   0(,%rax,4), %rcx
     00000000
0042 488B45E0        movq   -32(%rbp), %rax
0046 4801C8         addq   %rcx, %rax
0049 8B00            movl   (%rax), %eax
004b 39C2            cmpl   %eax, %edx
004d 7E0F            jle   .L3
004f 8B050000        movl   COUNT1(%rip), %eax
     0000
0055 83C001         addl   $1, %eax
0058 89050000        movl   %eax, COUNT1(%rip)
     0000
                .L3:
005e 8B45FC         movl   -4(%rbp), %eax
0061 4898            cltq
0063 488D1485        leaq   0(,%rax,4), %rdx
     00000000
006b 488B45E0        movq   -32(%rbp), %rax
006f 4801D0         addq   %rdx, %rax
0072 8B10            movl   (%rax), %edx
0074 8B45FC         movl   -4(%rbp), %eax
0077 4898            cltq
0079 488D0C85        leaq   0(,%rax,4), %rcx
     00000000
0081 488B45E8        movq   -24(%rbp), %rax
0085 4801C8         addq   %rcx, %rax
0088 8B00            movl   (%rax), %eax
008a 39C2            cmpl   %eax, %edx
008c 7E0F            jle   .L4
008e 8B050000        movl   COUNT2(%rip), %eax
     0000
0094 83C001         addl   $1, %eax
0097 89050000        movl   %eax, COUNT2(%rip)
     0000
                .L4:
009d 8345FC01        addl   $1, -4(%rbp)
                .L2:
00a1 8B45FC         movl   -4(%rbp), %eax
00a4 3B45DC         cmpl   -36(%rbp), %eax
00a7 0F8C72FF        jl   .L5
     FFFF
00ad 8B150000        movl   COUNT2(%rip), %edx
     0000
00b3 8B050000        movl   COUNT1(%rip), %eax
     0000
00b9 89C6            movl   %eax, %esi
00bb BF000000        movl   $.LC0, %edi
     00
00c0 B8000000        movl   $0, %eax
     00
00c5 E8000000        call   printf
     00
00ca C9              leave
                    .cfi_def_cfa 7, 8
00cb C3              ret
                    .cfi_endproc
                .LFE0:
                    .globl   main
                main:
                .LFB1:
                    .cfi_startproc
00cc 55              pushq   %rbp
                    .cfi_def_cfa_offset 16
                    .cfi_offset 6, -16
00cd 4889E5         movq   %rsp, %rbp
                    .cfi_def_cfa_register 6
00d0 4881ECB0        subq   $432, %rsp
     010000
00d7 C7855CFE        movl   $50, -420(%rbp)
     FFFF3200
     0000
00e1 C78558FE        movl   $0, -424(%rbp)
     FFFF0000
     0000
00eb EB34            jmp   .L7
                .L8:
011a 838558FE        addl   $1, -424(%rbp)
     FFFF01
00ed 8B8558FE        movl   -424(%rbp), %eax
     FFFF
00f3 4898            cltq
00f5 8B9558FE        movl   -424(%rbp), %edx
     FFFF
00fb 89948560        movl   %edx, -416(%rbp,%rax,4)
     FEFFFF
0102 8B8558FE        movl   -424(%rbp), %eax
     FFFF
0108 8D5005         leal   5(%rax), %edx
010b 8B8558FE        movl   -424(%rbp), %eax
     FFFF
0111 4898            cltq
0113 89948530        movl   %edx, -208(%rbp,%rax,4)
     FFFFFF
                .L7:
0121 83BD58FE        cmpl   $49, -424(%rbp)
     FFFF31
0128 7EC3            jle   .L8
012a 8B8558FF        movl   -168(%rbp), %eax
     FFFF
0130 83C00A         addl   $10, %eax
0133 898588FE        movl   %eax, -376(%rbp)
     FFFF
0139 8B856CFF        movl   -148(%rbp), %eax
     FFFF
013f 83C00A         addl   $10, %eax
0142 89859CFE        movl   %eax, -356(%rbp)
     FFFF
0148 8B4580         movl   -128(%rbp), %eax
014b 83C00A         addl   $10, %eax
014e 8985B0FE        movl   %eax, -336(%rbp)
     FFFF
0154 8B955CFE        movl   -420(%rbp), %edx
     FFFF
015a 488D8D30        leaq   -208(%rbp), %rcx
     FFFFFF
0161 488D8560        leaq   -416(%rbp), %rax
     FEFFFF
0168 4889CE         movq   %rcx, %rsi
016b 4889C7         movq   %rax, %rdi
016e E8000000        call   COMPAREV
     00
0173 B8000000        movl   $0, %eax
     00
0178 C9              leave
                    .cfi_def_cfa 7, 8
0179 C3              ret
                    .cfi_endproc
                .LFE1:
                .Letext0: