Assignment Description: An array of integers can be assigned to a memory address
ID: 668279 • Letter: A
Question
Assignment Description:
An array of integers can be assigned to a memory address in the .data section of a MIPS assembly language program as show below. Here the length of the array is stored first, and then the elements of the array numbersnext. A C program that will ask a user to enter two integers and compute the sum of numbers in the array that are between those two integers (inclusive). Implement a MIPS assembly language program to compute the sum of numbers in the array that are between the entered integers. If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it. Name your source code file assignment5.s.
.data
numbers_len: .word 14
numbers: .word 11, 24, 3, -6, 14, -18, 21, 45, 12, -27, 35, -7, 44, -28
Explanation / Answer
MIPS Assembly language code:
;The following program to compute the sum of numbers in the array by using given c program code
.Ltext0:
.LC0:
.string "Enter an integer:"
.LC1:
.string "%d"
.LC2:
.string "Enter another integer:"
.LC3:
.string "The sum of numbers that are in between: %d "
.p2align 4,,15
.globl main
main:
subq $88, %rsp
movl $11, 16(%rsp) ;int numbers[14] = {11, 24, 3, -6, 14, -18, 21, 45, 12, -27, 35, -7, 44, -28};
movl $24, 20(%rsp)
movl $3, 24(%rsp)
movl $-6, 28(%rsp)
movl $14, 32(%rsp)
movl $-18, 36(%rsp)
movl $21, 40(%rsp)
movl $45, 44(%rsp)
movl $12, 48(%rsp)
movl $-27, 52(%rsp)
movl $35, 56(%rsp)
movl $-7, 60(%rsp)
movl $44, 64(%rsp)
movl $-28, 68(%rsp)
movl $.LC0, %edi
.LVL0: ; printf("Enter an integer: ");
call puts
.LVL1:
leaq 8(%rsp), %rsi ;scanf("%d", &num1);
movl $.LC1, %edi
xorl %eax, %eax
call scanf
.LVL2: ;printf("Enter another integer: ");
movl $.LC2, %edi
call puts
.LVL3:
leaq 12(%rsp), %rsi ; scanf("%d", &num2);
xorl %eax, %eax
movl $.LC1, %edi
call scanf
.L2:
.LVL4: ;if (num1 < num2) operation
xorl %esi, %esi
jmp .L5
leaq 20(%rsp), %rax
leaq 72(%rsp), %rdi
movl $11, %edx
.LVL6:
.p2align 4,,10
.p2align 3
.L8:
movl (%rax), %edx
addq $4, %rax
.LVL7:
.L5: ; if (numbers[i] >= min && numbers[i] <= max)
cmpl %edx, %ecx
jg .L3
leal (%rsi,%rdx), %r8d ;sum = sum + numbers[i];
cmpl %edx, %r9d
cmovge %r8d, %esi
.LVL8:
.L3: ; for (i = 0; i < 14; i++) operaton
cmpq %rdi, %rax
jne .L8
movl $.LC3, %edi ;printf( "The sum of numbers that are inbetween: %d ", sum);
xorl %eax, %eax
call printf
.LVL9:
addq $88, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE0:
.text
.Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.