Assignment Description: Implement a MIPS assembly language program that defines
ID: 668338 • Letter: A
Question
Assignment Description:
Implement a MIPS assembly language program that defines main, readArray, printArray and reverseArray procedures.
The readArray takes an array of integers as its parameter, asks a user how many integers to read in and reads in integers from a user to fill the array. It also returns the number of integers in the array. Note that if you change the content of the array in the readArray procedure, then the main and other procedures will also see the new content of the array.
The reverseArray procedure takes parameters of an array of integers and its length, and rearranges the content of the array in the reverse order.
The printArray procedure takes parameters of an array of integers and its length, and print out the content of the array.
The main needs to ask a user to enter an integer to specify how many times to repeat, then within the loop, it calls readArray, reverseArray, and print Array. Please see the following C program to understand how it should work.
If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it.
Explanation / Answer
in assembly
.Ltext0:
.section .rodata
.align 8
.LC0:
0000 53706563 .string "Specify how many numbers should be stored in the array (at most 9):"
69667920
686F7720
6D616E79
206E756D
.LC1:
0044 256400 .string "%d"
.LC2:
0047 456E7465 .string "Enter an integer: "
7220616E
20696E74
65676572
3A2000
.text
.globl readArray
readArray:
.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 4883EC20 subq $32, %rsp
0008 48897DE8 movq %rdi, -24(%rbp)
000c C745FC00 movl $0, -4(%rbp)
000000
0013 BF000000 movl $.LC0, %edi
00
0018 E8000000 call puts
00
001d 488D45F4 leaq -12(%rbp), %rax
0021 4889C6 movq %rax, %rsi
0024 BF000000 movl $.LC1, %edi
00
0029 B8000000 movl $0, %eax
00
002e E8000000 call __isoc99_scanf
00
0033 EB3D jmp .L2
.L4:
0035 BF000000 movl $.LC2, %edi
00
003a E8000000 call puts
00
003f 488D45F8 leaq -8(%rbp), %rax
0043 4889C6 movq %rax, %rsi
0046 BF000000 movl $.LC1, %edi
00
004b B8000000 movl $0, %eax
00
0050 E8000000 call __isoc99_scanf
00
0055 8B45FC movl -4(%rbp), %eax
0058 4898 cltq
005a 488D1485 leaq 0(,%rax,4), %rdx
00000000
0062 488B45E8 movq -24(%rbp), %rax
0066 4801C2 addq %rax, %rdx
0069 8B45F8 movl -8(%rbp), %eax
006c 8902 movl %eax, (%rdx)
006e 8345FC01 addl $1, -4(%rbp)
.L2:
0072 8B45F4 movl -12(%rbp), %eax
0075 3945FC cmpl %eax, -4(%rbp)
0078 7D06 jge .L3
007a 837DFC08 cmpl $8, -4(%rbp)
007e 7EB5 jle .L4
.L3:
0080 8B45FC movl -4(%rbp), %eax
0083 C9 leave
.cfi_def_cfa 7, 8
0084 C3 ret
.cfi_endproc
.LFE0:
.section .rodata
.LC3:
005a 54686520 .string "The array has been reversed"
61727261
79206861
73206265
656E2072
.text
.globl reverseArray
reverseArray:
.LFB1:
.cfi_startproc
0085 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0086 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0089 4883EC20 subq $32, %rsp
008d 48897DE8 movq %rdi, -24(%rbp)
0091 8975E4 movl %esi, -28(%rbp)
0094 C745F400 movl $0, -12(%rbp)
000000
009b 8B45E4 movl -28(%rbp), %eax
009e 83C001 addl $1, %eax
00a1 89C2 movl %eax, %edx
00a3 C1EA1F shrl $31, %edx
00a6 01D0 addl %edx, %eax
00a8 D1F8 sarl %eax
00aa 8945F8 movl %eax, -8(%rbp)
00ad EB70 jmp .L7
.L8:
00af 8B45F4 movl -12(%rbp), %eax
00b2 4898 cltq
00b4 488D1485 leaq 0(,%rax,4), %rdx
00000000
00bc 488B45E8 movq -24(%rbp), %rax
00c0 4801D0 addq %rdx, %rax
00c3 8B00 movl (%rax), %eax
00c5 8945FC movl %eax, -4(%rbp)
00c8 8B45F4 movl -12(%rbp), %eax
00cb 4898 cltq
00cd 488D1485 leaq 0(,%rax,4), %rdx
00000000
00d5 488B45E8 movq -24(%rbp), %rax
00d9 4801C2 addq %rax, %rdx
00dc 8B45F4 movl -12(%rbp), %eax
00df 8B4DE4 movl -28(%rbp), %ecx
00e2 29C1 subl %eax, %ecx
00e4 89C8 movl %ecx, %eax
00e6 4898 cltq
00e8 48C1E002 salq $2, %rax
00ec 488D48FC leaq -4(%rax), %rcx
00f0 488B45E8 movq -24(%rbp), %rax
00f4 4801C8 addq %rcx, %rax
00f7 8B00 movl (%rax), %eax
00f9 8902 movl %eax, (%rdx)
00fb 8B45F4 movl -12(%rbp), %eax
00fe 8B55E4 movl -28(%rbp), %edx
0101 29C2 subl %eax, %edx
0103 89D0 movl %edx, %eax
0105 4898 cltq
0107 48C1E002 salq $2, %rax
010b 488D50FC leaq -4(%rax), %rdx
010f 488B45E8 movq -24(%rbp), %rax
0113 4801C2 addq %rax, %rdx
0116 8B45FC movl -4(%rbp), %eax
0119 8902 movl %eax, (%rdx)
011b 8345F401 addl $1, -12(%rbp)
.L7:
011f 8B45F4 movl -12(%rbp), %eax
0122 3B45F8 cmpl -8(%rbp), %eax
0125 7C88 jl .L8
0127 BF000000 movl $.LC3, %edi
00
012c E8000000 call puts
00
0131 C9 leave
.cfi_def_cfa 7, 8
0132 C3 ret
.cfi_endproc
.LFE1:
.section .rodata
.LC4:
0076 54686520 .string "The array content is:"
61727261
7920636F
6E74656E
74206973
.LC5:
008c 25640A00 .string "%d "
.text
.globl printArray
printArray:
.LFB2:
.cfi_startproc
0133 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0134 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0137 4883EC20 subq $32, %rsp
013b 48897DE8 movq %rdi, -24(%rbp)
013f 8975E4 movl %esi, -28(%rbp)
0142 C745FC00 movl $0, -4(%rbp)
000000
0149 BF000000 movl $.LC4, %edi
00
014e E8000000 call puts
00
0153 EB2B jmp .L10
.L11:
0155 8B45FC movl -4(%rbp), %eax
0158 4898 cltq
015a 488D1485 leaq 0(,%rax,4), %rdx
00000000
0162 488B45E8 movq -24(%rbp), %rax
0166 4801D0 addq %rdx, %rax
0169 8B00 movl (%rax), %eax
016b 89C6 movl %eax, %esi
016d BF000000 movl $.LC5, %edi
00
0172 B8000000 movl $0, %eax
00
0177 E8000000 call printf
00
017c 8345FC01 addl $1, -4(%rbp)
.L10:
0180 8B45FC movl -4(%rbp), %eax
0183 3B45E4 cmpl -28(%rbp), %eax
0186 7CCD jl .L11
0188 C9 leave
.cfi_def_cfa 7, 8
0189 C3 ret
.cfi_endproc
.LFE2:
.section .rodata
.align 8
.LC6:
0090 506C6561 .string "Please enter an integer to specify how many times to repeat:"
73652065
6E746572
20616E20
696E7465
.LC7:
00cd 4C6F6F70 .string "Loop %d "
2025640A
00
.text
.globl main
main:
.LFB3:
.cfi_startproc
018a 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
018b 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
018e 53 pushq %rbx
018f 4883EC28 subq $40, %rsp
.cfi_offset 3, -24
0193 4889E0 movq %rsp, %rax
0196 4889C3 movq %rax, %rbx
0199 C745D809 movl $9, -40(%rbp)
000000
01a0 8B45D8 movl -40(%rbp), %eax
01a3 4C63C0 movslq %eax, %r8
01a6 4983E801 subq $1, %r8
01aa 4C8945E0 movq %r8, -32(%rbp)
01ae 4C63C0 movslq %eax, %r8
01b1 4C89C6 movq %r8, %rsi
01b4 BF000000 movl $0, %edi
00
01b9 4863F0 movslq %eax, %rsi
01bc 4889F2 movq %rsi, %rdx
01bf B9000000 movl $0, %ecx
00
01c4 4898 cltq
01c6 48C1E002 salq $2, %rax
01ca 488D5003 leaq 3(%rax), %rdx
01ce B8100000 movl $16, %eax
00
01d3 4883E801 subq $1, %rax
01d7 4801D0 addq %rdx, %rax
01da B9100000 movl $16, %ecx
00
01df BA000000 movl $0, %edx
00
01e4 48F7F1 divq %rcx
01e7 486BC010 imulq $16, %rax, %rax
01eb 4829C4 subq %rax, %rsp
01ee 4889E0 movq %rsp, %rax
01f1 4883C003 addq $3, %rax
01f5 48C1E802 shrq $2, %rax
01f9 48C1E002 salq $2, %rax
01fd 488945E8 movq %rax, -24(%rbp)
0201 BF000000 movl $.LC6, %edi
00
0206 E8000000 call puts
00
020b 488D45D0 leaq -48(%rbp), %rax
020f 4889C6 movq %rax, %rsi
0212 BF000000 movl $.LC1, %edi
00
0217 B8000000 movl $0, %eax
00
021c E8000000 call __isoc99_scanf
00
0221 C745D400 movl $0, -44(%rbp)
000000
0228 EB6E jmp .L13
.L14:
022a 8B45D4 movl -44(%rbp), %eax
022d 83C001 addl $1, %eax
0230 89C6 movl %eax, %esi
0232 BF000000 movl $.LC7, %edi
00
0237 B8000000 movl $0, %eax
00
023c E8000000 call printf
00
0241 488B45E8 movq -24(%rbp), %rax
0245 4889C7 movq %rax, %rdi
0248 E8000000 call readArray
00
024d 8945DC movl %eax, -36(%rbp)
0250 488B45E8 movq -24(%rbp), %rax
0254 8B55DC movl -36(%rbp), %edx
0257 89D6 movl %edx, %esi
0259 4889C7 movq %rax, %rdi
025c E8000000 call reverseArray
00
0261 488B45E8 movq -24(%rbp), %rax
0265 8B55DC movl -36(%rbp), %edx
0268 89D6 movl %edx, %esi
026a 4889C7 movq %rax, %rdi
026d E8000000 call printArray
00
0272 488B45E8 movq -24(%rbp), %rax
0276 8B55DC movl -36(%rbp), %edx
0279 89D6 movl %edx, %esi
027b 4889C7 movq %rax, %rdi
027e E8000000 call reverseArray
00
0283 488B45E8 movq -24(%rbp), %rax
0287 8B55DC movl -36(%rbp), %edx
028a 89D6 movl %edx, %esi
028c 4889C7 movq %rax, %rdi
028f E8000000 call printArray
00
0294 8345D401 addl $1, -44(%rbp)
.L13:
0298 8B45D0 movl -48(%rbp), %eax
029b 3945D4 cmpl %eax, -44(%rbp)
029e 7C8A jl .L14
02a0 90 nop
02a1 4889DC movq %rbx, %rsp
02a4 488B5DF8 movq -8(%rbp), %rbx
02a8 C9 leave
.cfi_def_cfa 7, 8
02a9 C3 ret
.cfi_endproc
.LFE3:
.Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.