write assembly language programs to: -perform decision making using branch instr
ID: 668331 • Letter: W
Question
write assembly language programs to:
-perform decision making using branch instructions.
-create loops
-use syscall operations to display integers and strings on the console window
-use syscall operations to read integers from the keyboard.
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 numbers next. 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
The following shows how it looks like in a C program:
Explanation / Answer
=========================================================
Program in assembly language format
=========================================================
.file "arrays.c"
.section .rodata
.LC0:
.string "Enter an integer:"
.LC1:
.string "%d"
.LC2:
.string "Enter another integer:"
.align 8
.LC3:
.string "The sum of numbers that are inbetween: %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 $96, %rsp
movl $11, -80(%rbp)
movl $24, -76(%rbp)
movl $3, -72(%rbp)
movl $-6, -68(%rbp)
movl $14, -64(%rbp)
movl $-18, -60(%rbp)
movl $21, -56(%rbp)
movl $45, -52(%rbp)
movl $12, -48(%rbp)
movl $-27, -44(%rbp)
movl $35, -40(%rbp)
movl $-7, -36(%rbp)
movl $44, -32(%rbp)
movl $-28, -28(%rbp)
movl $0, -8(%rbp)
movl $.LC0, %edi
call puts
movl $.LC1, %eax
leaq -84(%rbp), %rdx
movq %rdx, %rsi
movq %rax, %rdi
movl $0, %eax
call __isoc99_scanf
movl $.LC2, %edi
call puts
movl $.LC1, %eax
leaq -88(%rbp), %rdx
movq %rdx, %rsi
movq %rax, %rdi
movl $0, %eax
call __isoc99_scanf
movl -84(%rbp), %edx
movl -88(%rbp), %eax
cmpl %eax, %edx
jge .L2
movl -84(%rbp), %eax
movl %eax, -16(%rbp)
movl -88(%rbp), %eax
movl %eax, -12(%rbp)
jmp .L3
.L2:
movl -88(%rbp), %eax
movl %eax, -16(%rbp)
movl -84(%rbp), %eax
movl %eax, -12(%rbp)
.L3:
movl $0, -4(%rbp)
jmp .L4
.L6:
movl -4(%rbp), %eax
cltq
movl -80(%rbp,%rax,4), %eax
cmpl -16(%rbp), %eax
jl .L5
movl -4(%rbp), %eax
cltq
movl -80(%rbp,%rax,4), %eax
cmpl -12(%rbp), %eax
jg .L5
movl -4(%rbp), %eax
cltq
movl -80(%rbp,%rax,4), %eax
addl %eax, -8(%rbp)
.L5:
addl $1, -4(%rbp)
.L4:
cmpl $13, -4(%rbp)
jle .L6
movl $.LC3, %eax
movl -8(%rbp), %edx
movl %edx, %esi
movq %rax, %rdi
movl $0, %eax
call printf
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
===============================================
Output:
===============================================
[~]$ gcc arrays.s -o arrays
[~]$ ./arrays
Enter an integer:
1
Enter another integer:
100
The sum of numbers that are inbetween: 209
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.