Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program In Assembly Description of Part (a): For this part, your MAL program mus

ID: 3580030 • Letter: P

Question

Program In Assembly

Description of Part (a): For this part, your MAL program must be in a file named p5a.mal. In the following discussion, a positive integer is one that is strictly greater than zero. Also, we say that an integer x evenly divides an integer y if the remainder when y is divided by x is zero. Recall that each integer is represented using 32 bits in MIPS. Assume that the bits are numbered right to left from 0 to 31. (Thus, the rightmost bit is numbered 0 and the leftmost bit is numbered 31.) We say that the bits 0 through 15 form the right half and the bits 16 through 31 form the left half of the integer. You are required to write a MAL program that prompts a user for a positive decimal integer reads the integer typed by the user and outputs the following values. (a) The total number of 1's in the left half of the binary representation of the integer. (b) The total number of 0's in the right half of the binary representation of the integer. (c) The highest power of 2 that evenly divides the integer. (d) The value of the smallest digit in the decimal representation of the integer. Example: Suppose the user types the decimal integer 1536. The 32-bit binary representation of 1536 is as follows: 0000000000000000 0000 0110 0000 0000 For this example, the required answers are as follows. (b) The number of 0's in the right half of the binary representation of the given integer 14

Explanation / Answer

Here is the program.. please check.

int_to_bin_digit(unsigned int, int, int*):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov QWORD PTR [rbp-32], rdx
mov eax, DWORD PTR [rbp-24]
sub eax, 1
mov edx, 1
mov ecx, eax
sal edx, cl
mov eax, edx
mov DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-4], 0
.L5:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-24]
jge .L6
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rdx, rax
mov eax, DWORD PTR [rbp-20]
and eax, DWORD PTR [rbp-8]
test eax, eax
je .L3
mov eax, 1
jmp .L4
.L3:
mov eax, 0
.L4:
mov DWORD PTR [rdx], eax
sal DWORD PTR [rbp-20]
add DWORD PTR [rbp-4], 1
jmp .L5
.L6:
nop
pop rbp
ret
.LC0:
.string "%d"
.LC1:
.string " Total left> .LC2:
.string " Total right> .LC3:
.string " Highest power of 2 that evenly divide is = %d"
main:
push rbp
mov rbp, rsp
sub rsp, 176
mov DWORD PTR [rbp-164], edi
mov QWORD PTR [rbp-176], rsi
mov DWORD PTR [rbp-24], 1536
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-12], 0
mov DWORD PTR [rbp-16], 1
mov DWORD PTR [rbp-20], 2
mov eax, DWORD PTR [rbp-24]
lea rdx, [rbp-160]
mov esi, 32
mov edi, eax
call int_to_bin_digit(unsigned int, int, int*)
mov DWORD PTR [rbp-4], 0
.L9:
cmp DWORD PTR [rbp-4], 31
jg .L8
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-160+rax*4]
mov esi, eax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
add DWORD PTR [rbp-4], 1
jmp .L9
.L8:
mov DWORD PTR [rbp-4], 0
.L12:
cmp DWORD PTR [rbp-4], 15
jg .L10
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-160+rax*4]
cmp eax, 1
jne .L11
add DWORD PTR [rbp-8], 1
.L11:
add DWORD PTR [rbp-4], 1
jmp .L12
.L10:
mov DWORD PTR [rbp-4], 16
.L15:
cmp DWORD PTR [rbp-4], 31
jg .L13
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-160+rax*4]
cmp eax, 1
jne .L14
add DWORD PTR [rbp-12], 1
.L14:
add DWORD PTR [rbp-4], 1
jmp .L15
.L13:
mov eax, DWORD PTR [rbp-24]
cdq
idiv DWORD PTR [rbp-20]
mov eax, edx
test eax, eax
jg .L20
sal DWORD PTR [rbp-20]
add DWORD PTR [rbp-16], 1
jmp .L13
.L20:
nop
mov eax, DWORD PTR [rbp-8]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-12]
mov esi, eax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
mov eax, DWORD PTR [rbp-16]
mov esi, eax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call printf
mov eax, 0
leave
ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote