Lab 5
Lab 5
𝑐 2 = 𝑎2 + 𝑏 2
where 𝑎, 𝑏, and 𝑐 are the lengths of three sides of the triangle.
1 .data
2 side_a: .quad 3
3 side_b: .quad 4
4 side_c: .quad 5
5 yes: .string "It is a right triangle.\n"
6 len_yes: .quad . - yes // Calculate the length of string yes
7 no: .string "It is not a right triangle.\n"
8 len_no: .quad . - no // Calculate the length of string no
where you can assume side_c is always the longest side. If it is a right triangle, you’d need to print the string
"It is a right triangle.\n " ; otherwise "It is not a right triangle.\n " .
To print a string, you might want to review and borrow the code from lab 2.
Requirements
Note your code is a complete assembly program (not just a sequence of instructions). It should be able
to assemble, link, and execute without error and warnings. When executed, the program should finish
without problems. If your code cannot assemble, you get no credit – this is the same as C programs that
cannot be compiled;
MUL instruction can be used for multiplications;
You can use either CBZ / CBNZ or B.EQ / B.NE for branching;
Avoid using registers X29 and X30;
You have to put comments on each line of instruction;
Put your name and honor code pledge at the top of your code in comments.
1 .text
2 .global _start
3 .extern scanf
1
4
5 _start:
6 ADR X0, fmt_str // Load address of formated string
7 ADR X1, left // Load &left
8 ADR X2, right // Load &right
9 ADR X3, target // Load &target
10 BL scanf // scanf("%ld %ld %ld", &left, &right, &target);
11
21 exit:
22 MOV X0, 0 // Pass 0 to exit()
23 MOV X8, 93 // Move syscall number 93 (exit) to X8
24 SVC 0 // Invoke syscall
25
26 .data
27 left: .quad 0
28 right: .quad 0
29 target: .quad 0
30 fmt_str: .string "%ld%ld%ld"
31 yes: .string "Target is in range\n"
32 len_yes: .quad . - yes // Calculate the length of string yes
33 no: .string "Target is not in range\n"
34 len_no: .quad . - no // Calculate the length of string no
The code given to you calls C library function scanf() to receive three signed integers from keyboard (seperated
by space or newline). The first and second integers represent a numerical range (left,right) (exclusive),
while the third integer represents a target number. Your code should check if target is inside the range. If
so, it should print the message labeled as yes ; otherwise no . You can assume left is always smaller than
right .
Again, to print messages, you’d need to invoke system calls learned in lab 3.
Because we used C library function here, during linking, you’d need flag -lc in the command. Related infor-
mation can be found in Appendix B.2.3.3 in the textbook.
Requirements
Note your code is a complete assembly program (not just a sequence of instructions). It should be able
to assemble, link, and execute without error and warnings. When executed, the program should finish
without problems. If your code cannot assemble, you get no credit – this is the same as C programs that
cannot be compiled;
Your code should complete the task successfully without errors. Please do check edge cases;
You have to put comments on each line of instruction;
Put your name and honor code pledge at the top of your code in comments.
2
3 Grading
The lab will be graded based on a total of 10 points, 5 for task 1 and 5 for task 2. The following lists deductibles,
and the lowest score is 0 – no negative scores:
Task 1:
• -5: the code does not assemble, or the program terminates abnormally/unsuccessfully;
• -5: the code is generated by compiler;
• -5: the code does not attempt the task;
• -5: the code cannot be explained clearly in person;
• -5: no output is printed;
• -5: not using conditionals;
• -3: the right triangle checking result is wrong;
• -1: one or more instructions is missing comments;
• -1: no pledge and/or name.
Task 2:
• -5:
the code does not assemble, or the program terminates abnormally/unsuccessfully;
• -5:
the code is generated by compiler;
• -5:
the code does not attempt the task;
• -5:
the code cannot be explained clearly in person;
• -5:
no output is printed;
• -5:
not using conditionals;
• -1:
1 point off for each of the following test cases:
* -5 6 3 ;
* -5 10 -30 ;
* -6 -2 0 ;
* 3 4 3;
* 10 30 30 ;
* -20 20 -20 ;
• -1: one or more instructions is missing comments;
• -1: no pledge and/or name.
Earlybird Extra Credit: 2% of extra credit will be given if the lab is finished by Wednesday 11:59PM EST (1 day
before the lab deadline). For specific policy, see syllabus.
Attendance: check off at the end of the lab to get attendance credit.
Deliverable
Assembly code for both tasks in two separate .s files. No need to zip all files; just submit both files
separately on Canvas.