EECS2021_SampleLabtest_V2

.docx

School

York University *

*We aren’t endorsed by this school

Course

2021

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

docx

Pages

2

Uploaded by MinisterJay1170

Report
EECS 2021: Sample Labtest Version 2 (Based on labs A-D) Q1 : Write an assembly program that i) inputs a positive integer n, ii) sums all positive integers smaller than n in a loop, and iii) outputs the obtained result. For verification calculate the sum directly using the expression (n*(n-1))/2 and output the resulting value. Here is a possible sample dialog (the user entered 20): sum(1..n-1) Enter n:20 sum(1..19)=190 (n*(n-1))/2=190 Compile and test your solution with different input values. Save your solution as a file named q1.asm for possible future use. Q2 : Write an assembly program that inputs a small positive integer and calculate the factorial ( n! ) of that integer. Here is a possible sample dialog (the user entered 3 so 3! was calculated): Enter n:3 3! = 6 Compile and test your solution with different input values. Save your solution as a file named q2.asm for possible future use. Solution Q1: s1: DC "sum(1..n-1) Enter n:\0" s2: DC "sum(1.." s3: DC ")=" s4: DC "(n*(n-1))/2=\0" addi x5, x0, s1 ecall x1, x5, 4 ;out question ecall x5, x0, 5 ;inp n addi x5, x5, -1 add x6, x0, x5 ;counter add x7, x0, x5 ;sum loop: addi x6, x6, -1 bge x0, x6, end add x7, x7, x6 beq x0, x0, loop end: ld x8, s2(x0)
ecall x1, x8, 3 ecall x1, x5, 0 ld x8, s3(x0) ecall x1, x8, 3 ecall x0, x7, 0 addi x8, x0, s4 ecall x1, x8, 4 ;out expr addi x6, x5, 1 mul x5, x5, x6 srli x5, x5, 1 ecall x1, x5, 0 Solution Q2: s1: DC "n! Enter n:\0" s2: DC "!=" st: DM 10 addi x5, x0, s1 ecall x1, x5, 4 ;out question ecall x5, x0, 5 ;inp n add x6, x0, x5 ;counter add x7, x0, x5 ;fact loop: addi x6, x6, -1 bge x0, x6, end mul x7, x7, x6 beq x0, x0, loop end: ecall x1, x5, 0 ld x8, s2(x0) ecall x1, x8, 3 ecall x1, x7, 0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help