CSC231 Homework 10
(c) --D. Thiebaut 17:43, 23 November 2008 (UTC)
The deadline for this assignment is Fri. Dec 5th, at 11:59 p.m. plus 1 minute. You may work in pairs on this assignment, in which case you can submit only one program with two names in the header.
Contents
Binary-Searching A Schedule of Dates and Actions
Goal
This assignment is on recursive search of an array. The array contains dates and actions. The dates are expressed in seconds, and the actions as letters of the alphabet.
Introduction
Here is an example of such an array:
section .data
dates dd 0 ; time 0 sec
db 'a' ; action a
dd 3600 ; time 1 hour
db 'b' ; action b
dd 36000 ; time 10 hours
db 'r'
at Time 0, i.e. midnight, Action 'a' has to be done. At Time 3600, or 1 hour after the previous action, Action 'b' has to be performed. At Time 36000, or 10 hours after Action 'a', Action 'r' has to be done. We assume that 'a', 'b' and 'r' can be tested by some function which will trigger Arduino actions, such as writing to pins, or reading values from pins and deciding what to do with the result.
What you will be searching is a time value, for example 3600. In this case you need to pass to your recursive function the number 3600, and it should return to you 'r'. If you search for 0, it will return 'a'. If you search for 123, it will return '\0' (the Ascii code equal to 0) to indicate that the search was not successful.
Getting the time of day in seconds
To figure out what the current time is, we can use the Linux System Call #78. It is call getTimeOfDay and is a standard linux low-level system call that most programming languages use. C, C++, Php among others have their own getTimeOfDay function that calls this interrupt.
Here's an example of how to use it in assembly:
%include "asm_io.inc"
;; -------------------------
;; data segment
;; -------------------------
section .data
tvsec dd 0 ; number of seconds since some epoch
tvusec dd 0 ; number of useconds
;; -------------------------
;; code area
;; -------------------------
section .text
global asm_main
asm_main:
;;; get the time of day in secs and print it
mov eax, 78 ; system call: get time of day
mov ebx, tvsec ; address of buffer for secs/usecs
mov ecx, 0 ; NULL for timezone
int 0x80
;;; print the number of seconds
mov eax, [tvsec]
call print_int
call print_nl
;;; print the number of microseconds
mov eax, [tvusec]
call print_int
call print_nl
;; return to C program
ret
You need two double-word variables that are next to each other (because the system call gets the address of the first one and stores 2 32-bit numbers in memory at that address). The system call will put the number of seconds in tvsec and the number of microseconds in tvusec. The number of microseconds is the number of microseconds since the last second started.
When I called this program on 11/23/08, I got this number for the number of seconds: 1227460058. This is definitely too big a number to represent the seconds since midnight. It turns out that if you convert this number of seconds to hours, days, or years, you get:
1227460058 sec 340961.1272 hours 14206.71363 days 38.92250311 years
Yes, 38 years! That's because Linux System Call 78 returns the number of seconds that have elapsed since January 1, 1970! No jokes. This has been this way for many years, and you can see that we have a few more years before we face another Y2K type problem.
Your assignment
Your assignment is to
- define an array of (seconds, action) tuples in memory,
- figure out the starting time of the execution of the program,
- get the time of day. Process it so that we have something more manageable than seconds since 01/01/1970,
- search the array of tuples recursively and print the action that corresponds to the current time, and
- stop the program when it has run for 20 seconds.
You'll have to make sure that you print each action once only. What I mean is that your program should have a loop, and in the loop will get the current time, search the array, find an action and print it. But this will take very little time and it possible that you may loop several time and find that the current time is 0, say. Your program should be clever enough to print the action corresponding to Time 0 only once.
Grading details
The Array
Use the following array in your program, please. It will make it easier to grade:
dates dd 0 ; time 0 sec
db 'a' ; action a
dd 1 ; time 1 sec (= 00:00:01 a.m.)
db 'b' ; action b
dd 2 ; time 2 sec
db 'r'
dd 3
db 'a'
dd 4
db 'c'
dd 5
db 'a'
dd 6
db 'd'
dd 7
db 'a'
dd 8
db 'b'
dd 9
db 'r'
dd 10
db 'a'
dd 11
db '!'
dd 12
db '1'
dd 13
db '2'
dd 14
db '3'
dd 15
db '4'
The Time of Day
When getting the time of day in seconds, simply AND it with 0x0000000f, this way you get a number of seconds ranging from 0 to 15, always.
Submission
submit hw10 hw10.asm
If you modified the driver.c program, please submit it as well.