Write an assembly language program that accumulates and reports the sales for th
ID: 3855193 • Letter: W
Question
Write an assembly language program that accumulates and reports the sales for the Albatross Corporation. The Albatross Company has four salespeople who sell five different products. The four salesperson are each identified by a number from 1 to 4. Each products is identified by a number from 1 to 5. Sales are reported by entering the salesperson’s number, the quantity sold, and the product number. For example, to report that salesperson 2 sold 100 units of product 4, you would enter 2 100 4. Your program must store the sales data in a two-dimensional array (4 rows by 5 columns). Each row represents a salesperson. Each column represents a product. Initialize the array to zeroes and allow your user to enter data. When the user enters a negative number for a salesman number, terminate the input phase of your program and display a report that shows the total sales for each salesperson and the total sales for each product.
Explanation / Answer
Here is a trick. You can write the program in C and convert it into Assembly language using the gcc compiler. If you are using gcc, you can get the assembly as gcc -S -o a.s a.c if your source code is 'a.c'.
#include <stdio.h>
int main() {
int personToProduct[4][5];
int i=0, j=0;
for (i=0; i<4; i++) {
for (j=0; j<5; j++) {
personToProduct[i][j] = 0;
}
}
int person, product, sales;
while(1) {
scanf("%d %d %d", &person, &sales, &product);
if (person <= 0) {
break;
}
personToProduct[person-1][product-1] = sales;
}
for (i=0; i<4; i++) {
int total = 0;
for (j=0; j<5; j++) {
total += personToProduct[i][j];
}
printf("Total sales of salesman %d, are %d ", i+1, total);
}
for (i=0; i<5; i++) {
int total = 0;
for (j=1; j<4; j++) {
total += personToProduct[j][i];
}
printf("Total sales of product %d are %d ", i+1, total);
}
return 0;
}
Assembly Code
.file "program.c"
.section .rodata
.LC0:
.string "%d %d %d"
.align 8
.LC1:
.string "Total sales of salesman %d, are %d "
.align 8
.LC2:
.string "Total sales of product %d are %d "
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $112, %rsp
movl $0, -96(%rbp)
movl $0, -92(%rbp)
movl $0, -96(%rbp)
jmp .L2
.L5:
movl $0, -92(%rbp)
jmp .L3
.L4:
movl -92(%rbp), %eax
movslq %eax, %rcx
movl -96(%rbp), %eax
movslq %eax, %rdx
movq %rdx, %rax
salq $2, %rax
addq %rdx, %rax
addq %rcx, %rax
movl $0, -80(%rbp,%rax,4)
addl $1, -92(%rbp)
.L3:
cmpl $4, -92(%rbp)
jle .L4
addl $1, -96(%rbp)
.L2:
cmpl $3, -96(%rbp)
jle .L5
.L8:
leaq -104(%rbp), %rcx
leaq -100(%rbp), %rdx
leaq -108(%rbp), %rax
movq %rax, %rsi
movl $.LC0, %edi
movl $0, %eax
call __isoc99_scanf
movl -108(%rbp), %eax
testl %eax, %eax
jg .L6
nop
movl $0, -96(%rbp)
jmp .L9
.L6:
movl -108(%rbp), %eax
leal -1(%rax), %edx
movl -104(%rbp), %eax
subl $1, %eax
movl -100(%rbp), %ecx
movslq %eax, %rsi
movslq %edx, %rdx
movq %rdx, %rax
salq $2, %rax
addq %rdx, %rax
addq %rsi, %rax
movl %ecx, -80(%rbp,%rax,4)
jmp .L8
.L12:
movl $0, -88(%rbp)
movl $0, -92(%rbp)
jmp .L10
.L11:
movl -92(%rbp), %eax
movslq %eax, %rcx
movl -96(%rbp), %eax
movslq %eax, %rdx
movq %rdx, %rax
salq $2, %rax
addq %rdx, %rax
addq %rcx, %rax
movl -80(%rbp,%rax,4), %eax
addl %eax, -88(%rbp)
addl $1, -92(%rbp)
.L10:
cmpl $4, -92(%rbp)
jle .L11
movl -96(%rbp), %eax
leal 1(%rax), %ecx
movl -88(%rbp), %eax
movl %eax, %edx
movl %ecx, %esi
movl $.LC1, %edi
movl $0, %eax
call printf
addl $1, -96(%rbp)
.L9:
cmpl $3, -96(%rbp)
jle .L12
movl $0, -96(%rbp)
jmp .L13
.L16:
movl $0, -84(%rbp)
movl $1, -92(%rbp)
jmp .L14
.L15:
movl -96(%rbp), %eax
movslq %eax, %rcx
movl -92(%rbp), %eax
movslq %eax, %rdx
movq %rdx, %rax
salq $2, %rax
addq %rdx, %rax
addq %rcx, %rax
movl -80(%rbp,%rax,4), %eax
addl %eax, -84(%rbp)
addl $1, -92(%rbp)
.L14:
cmpl $3, -92(%rbp)
jle .L15
movl -96(%rbp), %eax
leal 1(%rax), %ecx
movl -84(%rbp), %eax
movl %eax, %edx
movl %ecx, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
addl $1, -96(%rbp)
.L13:
cmpl $4, -96(%rbp)
jle .L16
movl $0, %eax
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
.section .note.GNU-stack,"",@progbits
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.