Difference between revisions of "CSC231 Homework 3 Fall 2017"
(One intermediate revision by the same user not shown) | |||
Line 55: | Line 55: | ||
=Problem 2= | =Problem 2= | ||
<br /> | <br /> | ||
− | * Answer the quiz on Moodle, in the Homework 3 Problem 2 section. | + | * Answer the quiz on Moodle, in the Homework 3 Problem 2 section. This quiz refers to the program you wrote in Problem 1. |
<br /> | <br /> | ||
<br /> | <br /> | ||
<showafterdate after="20171012 12:00" before="20171231 00:00"> | <showafterdate after="20171012 12:00" before="20171231 00:00"> | ||
+ | =Solution Program= | ||
::<source lang="asm"> | ::<source lang="asm"> | ||
+ | ;;; ; program_name.asm | ||
+ | ;;; ; your name | ||
+ | ;;; ; | ||
+ | ;;; ; a description of the program | ||
+ | ;;; ; | ||
+ | ;;; ; to assemble and run: | ||
+ | ;;; ; | ||
+ | ;;; ; nasm -f elf -F stabs program.asm | ||
+ | ;;; ; ld -melf_i386 -o program program.o | ||
+ | ;;; ; ./program | ||
+ | ;;; ; ------------------------------------------------------------------- | ||
+ | |||
+ | ;; %include files here... | ||
+ | extern _printInt | ||
+ | extern _getInput | ||
+ | extern _printString | ||
+ | extern _println | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; data areas | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | |||
+ | section .data | ||
+ | seconds dd 0 | ||
+ | days dd 0 | ||
+ | hours dd 0 | ||
+ | min dd 0 | ||
+ | sec dd 0 | ||
+ | |||
+ | prompt db "> " | ||
+ | secStrings db " secs " | ||
+ | daysString db " days " | ||
+ | hoursString db " hours " | ||
+ | minutesString db " minutes " | ||
+ | secondsString db " seconds " | ||
+ | |||
+ | ;;; ------------------------------------------------------------ | ||
+ | ;;; code area | ||
+ | ;;; ------------------------------------------------------------ | ||
+ | |||
+ | section .text | ||
+ | global _start | ||
+ | _start: | ||
+ | ;;; get number of seconds | ||
+ | mov ecx, prompt | ||
+ | mov edx, 2 | ||
+ | call _printString | ||
+ | call _getInput | ||
+ | mov dword[seconds], eax | ||
+ | |||
+ | ;;; compute # of minutes and left-over seconds | ||
+ | mov edx, 0 | ||
+ | mov ebx, 60 | ||
+ | div ebx | ||
+ | mov dword[sec],edx | ||
+ | mov edx, 0 ; edx:eax contains # of minutes | ||
+ | |||
+ | ;;; compute # of hours and left-over minutes | ||
+ | div ebx | ||
+ | mov dword[min], edx | ||
+ | mov edx, 0 ; edx:eax contains # of hours | ||
+ | |||
+ | ;;; compute # of days and left-over hours | ||
+ | mov ebx, 24 | ||
+ | div ebx | ||
+ | mov dword[hours], edx | ||
+ | mov dword[days], eax | ||
+ | |||
+ | ;;; print the whole day, hours, minutes, seconds string | ||
+ | mov eax, dword[days] | ||
+ | call _printInt | ||
+ | mov ecx, daysString | ||
+ | mov edx, 6 | ||
+ | call _printString | ||
+ | |||
+ | mov eax, dword[hours] | ||
+ | call _printInt | ||
+ | mov ecx, hoursString | ||
+ | mov edx, 7 | ||
+ | call _printString | ||
+ | |||
+ | mov eax,dword[min] | ||
+ | call _printInt | ||
+ | mov ecx, minutesString | ||
+ | mov edx, 9 | ||
+ | call _printString | ||
+ | |||
+ | mov eax, dword[sec] | ||
+ | call _printInt | ||
+ | mov ecx, secondsString | ||
+ | mov edx, 9 | ||
+ | call _printString | ||
+ | call _println | ||
+ | ;;; exit() | ||
+ | |||
+ | mov eax,1 | ||
+ | mov ebx,0 | ||
+ | int 0x80 ; final system call | ||
+ | |||
</source> | </source> |
Latest revision as of 16:46, 7 October 2017
--D. Thiebaut (talk) 17:26, 1 October 2017 (EDT)
This assignment is due on Monday 10/11/17 at 11:55 p.m. You can work on this assignment in pairs, in which case you must include both your names in the header of the assembly language program you submit. Both members of a pair must submit a program and answer the quiz.
Contents
Problem 1
Assignment
- Write an assembly language program that you will link with the 231Lib library, and that will prompt the user for an integer (positive) number of seconds, which it will then translate in a number of days, hours, minutes and seconds.
- Call your program hw3.asm.
- The format of your output should be similar to the format illustrated in the examples shown below.
- The capture below shows how your program should be behaving.
cs231a@aurora ~/ $ ./hw3 > 1 0 days 0 hours 0 minutes 1 seconds cs231a@aurora ~/ $ ./hw3 > 0 0 days 0 hours 0 minutes 0 seconds cs231a@aurora ~/ $ ./hw3 > 3601 0 days 1 hours 0 minutes 1 seconds cs231a@aurora ~/ $ ./hw3 > 8641 0 days 2 hours 24 minutes 1 seconds cs231a@aurora ~/ $ ./hw3 > 86401 1 days 0 hours 0 minutes 1 seconds cs231a@aurora ~/ $ 864001 864001: command not found cs231a@aurora ~/ $ ./hw3 > 864001 10 days 0 hours 0 minutes 1 seconds cs231a@aurora ~/ $
Additional Information
- Use the DIV instruction
- Use ints (declared with dd) to store the different quantities your program will compute, including the original number of seconds provided by the user.
- Do not worry about whether words should be singular or plural. We do not know how to test quantities yet. Make your program output all the words in their plural form.
- Make your program output all the quantities on one line.
- You can use WolfranAlpha to convert seconds to day/hours/minutes/seconds, to test your program. WolframAlpha will correctly understand what to do if you enter something like "864001 seconds" in the search bar.
Submission
- Document your program well, and submit it on Moodle, in the Homework 3, Problem 1 section. Be careful as Moodle gives only 5-minute increments for the closing time, which means you have until 11:55 p.m. on Monday to submit the program.
Problem 2
- Answer the quiz on Moodle, in the Homework 3 Problem 2 section. This quiz refers to the program you wrote in Problem 1.
<showafterdate after="20171012 12:00" before="20171231 00:00">
Solution Program
;;; ; program_name.asm ;;; ; your name ;;; ; ;;; ; a description of the program ;;; ; ;;; ; to assemble and run: ;;; ; ;;; ; nasm -f elf -F stabs program.asm ;;; ; ld -melf_i386 -o program program.o ;;; ; ./program ;;; ; ------------------------------------------------------------------- ;; %include files here... extern _printInt extern _getInput extern _printString extern _println ;;; ------------------------------------------------------------ ;;; data areas ;;; ------------------------------------------------------------ section .data seconds dd 0 days dd 0 hours dd 0 min dd 0 sec dd 0 prompt db "> " secStrings db " secs " daysString db " days " hoursString db " hours " minutesString db " minutes " secondsString db " seconds " ;;; ------------------------------------------------------------ ;;; code area ;;; ------------------------------------------------------------ section .text global _start _start: ;;; get number of seconds mov ecx, prompt mov edx, 2 call _printString call _getInput mov dword[seconds], eax ;;; compute # of minutes and left-over seconds mov edx, 0 mov ebx, 60 div ebx mov dword[sec],edx mov edx, 0 ; edx:eax contains # of minutes ;;; compute # of hours and left-over minutes div ebx mov dword[min], edx mov edx, 0 ; edx:eax contains # of hours ;;; compute # of days and left-over hours mov ebx, 24 div ebx mov dword[hours], edx mov dword[days], eax ;;; print the whole day, hours, minutes, seconds string mov eax, dword[days] call _printInt mov ecx, daysString mov edx, 6 call _printString mov eax, dword[hours] call _printInt mov ecx, hoursString mov edx, 7 call _printString mov eax,dword[min] call _printInt mov ecx, minutesString mov edx, 9 call _printString mov eax, dword[sec] call _printInt mov ecx, secondsString mov edx, 9 call _printString call _println ;;; exit() mov eax,1 mov ebx,0 int 0x80 ; final system call
</showafterdate>