Timer & Counter
Timer & Counter
Timer & Counter
0x00500
0040:006C tick_count
total_ticks_today
total_seconds_today =
number of ticks-per-second
GATE2 OUT2
Channel 2 speaker
AND
COUNT REGISTER
CLK
MSB LSB
OUT
MSB LSB
LATCH REGISTER
GATE
STATUS
TIMER/COUNTER CHANNEL
8254 Command-Port
7 6 5 4 3 2 1 0
binary
CHANNEL COMMAND OUTPUT MODE
/ BCD
32-bit operands
product (64-bits)
EDX EAX
64-bit product
How ‘DIV’ works
Before executing the DIV instruction…
dividend (64-bits)
EDX EAX
64-bit dividend
32-bit operand
HH:MM:SS am/pm
– total_minutes = ( total_seconds / 60 );
ss = ( total_seconds % 60 );
– total_hours = (total_minutes / 60 );
mm = ( total_minutes % 60 );
– total_halfdays = (total_hours / 12 );
hh = (total_hours % 12 );
– Total_days = ( total_halfdays / 2 );
xm = total_halfdays % 2;
A subtle refinement
• Our ‘total_seconds’ value was gotten with
an integer-division operation, so there’s
likely to be some ‘round-off’ error
• How can we be sure we use the ‘closest’
integer to the actual quotient?
• We should remember the ‘rounding’ rule!
• When ‘remainder’ is equal or greater than
1/2 of ‘divisor’, ‘quotient’ gets incremented
How to implement rounding?
• There is more than one way to do it – i.e.,
the “amateur’s” way or the “expert’s” way
• Knowledge of the Pentium’s architecture
and instruction-set can assist
• The ‘obvious’ method:
• if ( 2 * remainder >= divisor ) ++quotient;
• But this uses a multiply and a conditional
jump-instruction (inefficient!)
Avoiding inefficiency…
• Replace the ‘multiply’ with an ‘addition’
• Use ‘subtract’ and ‘add-with-carry’ instead
of using ‘compare’ and ‘conditionally-jump’
# Recall: quotient was in EAX, remainder was in EDX, divisor was in ECX
# So this achieves the same effect as the ‘rounding rule’, but wit no jump!
In-class exercise
• Can you enhance our ‘timeoday.s’ demo
to make it more dramatic (and later useful)
by creating a loop within its ‘main’ routine,
so it continues to read and display the time
(until the user presses a key)
• HINTS: Use an INT-0x16 keyboard service
to ‘peek’ into the keyboard-queue, and
omit the ‘\n’ (newline) control-code from
the ‘report’ message-string