how to user a timer in assembler?

stickybytes

Golden Member
Sep 3, 2003
1,043
0
0
Hey guys,

I am in need of some serious help with an assembly language assignment for class that involves the use of graphics. Basically, im trying to write a program that displays two objects on the screen that are moving up at different rates. Does anyone know how i can implement a timer to do this ? Is anyone aware of which interrupts i need to use.

Thanks.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
INT 8 / 1C fire 18.2x per second. INT 70 fires 1024 times per second. INT 8 / 1C can be reprogrammed to fire faster. http://www.sat.dundee.ac.uk/~psc/pctim003.txt
(sections 7.36, 7.37, 8, 10.2 look useful). I've never done this myself.

If you're running under a non-trivial OS, you probably can't use these directly, and need to use system calls for your timers.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I used to reprogram int 8 a lot in the past. Basically you have to know the port setup of the 8254PIC and put the right values in the countdown register for channel 0. You also then need to implement your interrupt handler to chain the prior handler at the original 18.2 hz rate (to keep the O/S happy), and as CTho says you may be running in a virtual machine when you try this, and your results may vary.

I still have assembler code (embedded into C++) that does what you need with the timer and the interrupts. Shoot me a PM with an email and I will send it to you. You probably can't build it directly (was Borland C++ 3.x or something if I recall) but it will point you in the right direction.

By the way, you didn't say anything about platform. If you're running under Windows you can still make calls from assembler into Windows system services, so that may be a better option than messing with the hardware timer. Unfortunately importing API libraries into .asm programs and correctly setting up the calls is beyond my ability :).
 

stickybytes

Golden Member
Sep 3, 2003
1,043
0
0
Thanks for all the help thus far guys. My assembler program needs to work under windows XP. Furthermore, i will be programming in the text mode of assembler.

I've looked through the links you guys have provided thus far and the coding for a timer seems to be pretty involved. Do you guys have any other suggestions as to how to create different delay rates for graphics that occur concurrently? Basically what i'm trying to create is a game that has objects moving up to the top of the screen. You basically control a paddle on the bottom that moves side to side and you need to fire a bullet into the object before the objects reach the top of the screen. The problem im running into is the different timings of all the objects. The bullet obviously needs to move at a faster rate than the flames but at the same time, I need to slow the game down so that the user has a chance to respond. Does anyone have any ideas as to what's available for me in assembler to let me do this?

Thanks.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Given those requirements, I can think of a few ways, but unfortunately I suspect they're part of what you're supposed to be figuring out yourself as part of your assignment. Think about how you'd do it in another programming language, though.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
If you want everything to run at the right relative speeds, that's one challenge. If you want everything to run at the same speed on different processors, that's another. Anyway, you don't really have to get into reprogramming the timer, there's enough resolution there for what you want to do I think. Just poll it.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Most console games run at 30 or 60 screen updates / frames per second, and 60 updates / second is fine for PC games as well.

A common approach to this problem is:

1. code that updates the position and state of all objects for one "time slice" such as 1/60 second.
Different movement rates are handled by adjusting the stored x-y values of different objects by different values

critter_x += critter_move_per_60sec_x

missile_x += missile_move_per_60sec_x

2. code that updates an offscreen frame buffer based on the new world state generated in 1.

3. interrupt that triggers screen refresh using the buffer from 2.

Back in the Atari & C=64 days we updated the screen when the "vertical blank" interrupt was fired by the display hardware, to prevent "tearing" of the screen.

For a simple class assignment it's probably OK to either set a timer interrupt, or even to loop and poll the clock and wait until 1/60 second has passed since the last screen update.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
http://www.fastgraph.com/makeg...escroller/chapt12.html
Look for the timer.asm listing. Basic idea is you increase the rate that the hardware timer calls the interrupt (what the port 40h and 43h stuff is all about). But in order to make the system happy you have to hook the interrupt handler to do two things: increment your time variable at the increased rate, and also call the old handler at the same 18.2 rate. So say you increase the timer interrupt rate by 10, every 10 calls to your interrupt handler you'll have to call the old handler too (by saving it's address before you hook it with your own).

I don't know if the XP CMD emulates int 08h and the timer I/O property though, I haven't worked on any DOS programs in XP.

Are you using a DOS compiler and writing real DOS code or a Win32 compiler targeting the command prompt?

The QueryPerformanceCounter and it's associated API are the functions you want to look at for native Win32.

Another way of doing it without going fixed step is something along the lines of (using the above example):

critter_x += critter_move_per_sec_x * delta_time;