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

Given a year, write down the assembly program to determine whether it is leap or

ID: 3543345 • Letter: G

Question

Given a year, write down the assembly program to determine whether it is leap

or not as follows:

if (year is divisible by 400) then

is_leap_year

else if (year is divisible by 100) then

not_leap_year

else if (year is divisible by 4) then

is_leap_year

else

not_leap_year

[Use the rem instruction for the modulo operation. Indicate the result by storing the

either 0/1 in the register, 1 to indicate leap year and 0 otherwise.]

[Try to minimize the nop instructions by substituting it with one of the instructions

from the code. Make sure it doesn

Explanation / Answer

    .section ".text"
    .global main
    main:
    save %sp, -96, %sp
    set instr, %o0
    set [data], %o1
    set [enterkey], %o2
    call scanf
    nop
    mov      instr, %o0       ! Parameter num goes to %o0
    mov      400, %l0         ! 400 goes to local register           call     .rem           ! Call modulus subroutine
   nop

   mov       %l0,%o0       ! moves the result of the subroutine
                            ! to output register o0
   cmp   %o0, 0
    bne   else_part
    nop
    then_part:
    set outstr, %o0
  
    call printf
    nop
   ba   next
   nop
   else_part:
   nop
    mov      instr, %o0       ! Parameter num goes to %o0
    mov      100, %l0         ! 400 goes to local register           call     .rem           ! Call modulus subroutine
   nop

   mov       %l0,%o0       ! moves the result of the subroutine
                            ! to output register o0
   cmp   %o0, 0
    bne   else_part1
    nop
    then_part1:
    set outstr1, %o0
  
    call printf
    nop
   ba   next
   nop
    nop
    else_part1:
    nop
    mov      instr, %o0       ! Parameter num goes to %o0
    mov      4, %l0         ! 400 goes to local register           call     .rem           ! Call modulus subroutine
   nop

   mov       %l0,%o0       ! moves the result of the subroutine
                            ! to output register o0
   cmp   %o0, 0
    bne   else_part2
    nop
    then_part2:
    set outstr, %o0

    call printf
    nop
   ba   next
   nop
   else_part2:
   set outstr1, %o0
  
    call printf
    nop
   ba   next
   next:
   nop
    ret
    restore
    .section ".data"
    instr: .asciz "%d"
    outstr: .asciz "leap year: "
    outstr1: .asciz "not a leap year: "
  
    .align 4
    data: .word
    enterkey: .byte

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