This assignment involves the calculation of a vector inner product in a max-min
ID: 3553207 • Letter: T
Question
This assignment involves the calculation of a vector inner product in a
max-min algebra (also referred to as the bottleneck algebra). The ordinary
algebraic inner product of two vectors X and Y is defined by
X +.* Y = x[1]* y[1] + x[2]*y[2] + ... x[n]*y[n]
It is the sum of the item-wise products. The max-min inner product is
defined similarly as
X max.min Y = (x[1] min y[1]) max (x[2] min y[2]) max ... (x[n] min y[n])
and so it is just the maximum of the item-wise minimums.
he following C function describes the calculation of this inner product.
int maxminip( int *x, int *y, int n) /* x and y are pointers */
{
int i, y, m = -32768;
for( i = 0; i < n; i++) /* note the use of pointer arithmetic, rather
than indices, in the loop (as in AL) */
{
if (*x < *y) /* *x signifies dereferencing of the pointer value, */
t = *x; /* which corresponds to register indirect mode in AL */
else
t = *y;
if (t > m)
m = t;
x++; /* update the pointers; the C compiler adds the correct value */
y++; /* to the pointers depending on the type pointed to */
}
return m;
}
Translate this function into assembly language in a separately assembled
module. The first two parameters, a pointer to x and a pointer to y, are
are to be passed in registers SI and DI respectively, while the parameter
n (the length of the two vectors) is to be passed in register CX. The
function result is returned in register AX, and register BX is assigned
to t. This leaves register BP available as a temporary.
A translation of the following procedure should serve to test maxminip.
procedure maxmntst;
int i, n;
int x[ 100], y[ 100];
loop
write 'n> ';
read n;
exit when n <= 0;
write cr, lf, 'x>', cr, lf;
read x; /* use the procedure GetVec in /Examples */
write 'y>', cr, lf;
read y; /* use GetVec */
write 'ip = ', maxminip( &x, &y, n), cr, lf
endloop
end maxmntst;
Explanation / Answer
Here is the assembly code:
0016 C745F400 movl $0, -12(%rbp)
000000
001d EB40 jmp .L2
.L6:
001f 488B45E8 movq -24(%rbp), %rax
0023 8B10 movl (%rax), %edx
0025 488B45E0 movq -32(%rbp), %rax
0029 8B00 movl (%rax), %eax
002b 39C2 cmpl %eax, %edx
002d 7D0B jge .L3
002f 488B45E8 movq -24(%rbp), %rax
0033 8B00 movl (%rax), %eax
0035 8945F8 movl %eax, -8(%rbp)
0038 EB09 jmp .L4
.L3:
003a 488B45E0 movq -32(%rbp), %rax
003e 8B00 movl (%rax), %eax
0040 8945F8 movl %eax, -8(%rbp)
.L4:
0043 8B45F8 movl -8(%rbp), %eax
0046 3B45FC cmpl -4(%rbp), %eax
0049 7E06 jle .L5
004b 8B45F8 movl -8(%rbp), %eax
004e 8945FC movl %eax, -4(%rbp)
.L5:
005b 8345F401 addl $1, -12(%rbp)
0051 488345E8 addq $4, -24(%rbp)
04
0056 488345E0 addq $4, -32(%rbp)
04
.L2:
005f 8B45F4 movl -12(%rbp), %eax
0062 3B45DC cmpl -36(%rbp), %eax
0065 7CB8 jl .L6
0067 8B45FC movl -4(%rbp), %eax
006a 5D popq %rbp
.cfi_def_cfa 7, 8
006b C3 ret
.cfi_endproc
.LFE0:
.Letext0: