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

The answer must be in WinMIPS64 code. Please don\'t respond with any other code.

ID: 3803875 • Letter: T

Question

The answer must be in WinMIPS64 code. Please don't respond with any other code.

Write a WinMIPS64 code to determine whether a given number is an Armstrong number. The n-digit numbers equal to sum of nth powers of their digits (a finite sequence), called Armstrong numbers. They first few are given by 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748,….

For example, consider 371, 371 = 33 + 73+ 13 = 27 + 343 + 1 = 371. A sample C code for your assistance is given below:

#include <stdio.h>

#include <math.h>

int main(){

int num,i;

int digit,sum,capture;

sum = 0.0;

capture = 0.0;

printf( "Enter the total number of digits " );

scanf( "%d", &num );

for( i = 0; i < num; i++ ) {

        printf( "Enter the %d digit: ", i+1 );

        scanf( "%d", &digit );

        sum = sum + pow(digit,num);

        capture = capture * 10 + digit;

}

printf( "Entered Number: %d ", capture );

printf( "Sum of Product: %d ", sum );

if( capture == sum )

        printf( "Armstrong number" );

else

        printf( "Not an Armstrong number" );

}

All examples can be used for manipulation. Note that the numbers displayed are in floating point. Display the numbers using double format in WinMIPS64. You need to test the code only for 1-4 digit numbers from 0 to 9999. It is sufficient to follow the mentioned C logic that checks for 4-digit Armstrong numbers, even though more digits can be specified. Provide a complete report of your code i.e. stall count, code size, CPI.

Explanation / Answer

Answer:

MIPS Assembly Language Code:

push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     DWORD PTR [rbp-4], edi
        mov     DWORD PTR [rbp-8], esi
        pxor    xmm1, xmm1
        cvtsi2sd        xmm1, DWORD PTR [rbp-8]
        pxor    xmm0, xmm0
        cvtsi2sd        xmm0, DWORD PTR [rbp-4]
        call    pow
        movq    rax, xmm0
        mov     QWORD PTR [rbp-16], rax
        movsd   xmm0, QWORD PTR [rbp-16]
        leave
        ret
.LC0:
        .string "Enter the total number of digits"
.LC1:
        .string "%d"
.LC2:
        .string "Enter the %d digit: "
.LC3:
        .string "Entered Number: %d "
.LC4:
        .string "Sum of Product: %d "
.LC5:
        .string "Armstrong number"
.LC6:
        .string "Not an Armstrong number"
main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 48
        mov     DWORD PTR [rbp-8], 0
        mov     DWORD PTR [rbp-12], 0
        mov     edi, OFFSET FLAT:.LC0
        call    puts
        lea     rax, [rbp-16]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:.LC1
        mov     eax, 0
        call    scanf
        mov     DWORD PTR [rbp-4], 0
.L5:
        mov     eax, DWORD PTR [rbp-16]
        cmp     DWORD PTR [rbp-4], eax
        jge     .L4
        mov     eax, DWORD PTR [rbp-4]
        add     eax, 1
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC2
        mov     eax, 0
        call    printf
        lea     rax, [rbp-20]
        mov     rsi, rax
        mov     edi, OFFSET FLAT:.LC1
        mov     eax, 0
        call    scanf
        pxor    xmm1, xmm1
        cvtsi2sd        xmm1, DWORD PTR [rbp-8]
        movsd   QWORD PTR [rbp-40], xmm1
        mov     edx, DWORD PTR [rbp-16]
        mov     eax, DWORD PTR [rbp-20]
        mov     esi, edx
        mov     edi, eax
        call    __gnu_cxx::__promote_2<int, int, __gnu_cxx::__promote<int, std::__is_integer<int>::__value>::__type, __gnu_cxx::__promote<int, std::__is_integer<int>::__value>::__type>::__type std::pow<int, int>(int, int)
        addsd   xmm0, QWORD PTR [rbp-40]
        cvttsd2si       eax, xmm0
        mov     DWORD PTR [rbp-8], eax
        mov     edx, DWORD PTR [rbp-12]
        mov     eax, edx
        sal     eax, 2
        add     eax, edx
        add     eax, eax
        mov     edx, eax
        mov     eax, DWORD PTR [rbp-20]
        add     eax, edx
        mov     DWORD PTR [rbp-12], eax
        add     DWORD PTR [rbp-4], 1
        jmp     .L5
.L4:
        mov     eax, DWORD PTR [rbp-12]
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC3
        mov     eax, 0
        call    printf
        mov     eax, DWORD PTR [rbp-8]
        mov     esi, eax
        mov     edi, OFFSET FLAT:.LC4
        mov     eax, 0
        call    printf
        mov     eax, DWORD PTR [rbp-12]
        cmp     eax, DWORD PTR [rbp-8]
        jne     .L6
        mov     edi, OFFSET FLAT:.LC5
        mov     eax, 0
        call    printf
        jmp     .L7
.L6:
        mov     edi, OFFSET FLAT:.LC6
        mov     eax, 0
        call    printf
.L7:
        mov     eax, 0
        leave
        ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote