Title: Lecture 10: a New Interrupt invoking function
1Lecture 10 a New Interrupt invoking function
- Software Engineering
- pointers and structures
- Embedded Systems
- A new interrupt invoking function
2Pointers
- Syntax
- data_type pointer_name
- An example
- int my_pointer //my_pointer is a pointer to an
integer - char another_pointer //another_pointer is a
pointer to a char - //it can also used as an array of char
- another_pointerDeng //
- cout ltlt another_pointer ltlt endl
- //it is equivalent to coutltltDengltltendl
3Pointers
- int my_pointer
- my_pointer name of the pointer
- my_pointer address of the pointer
- my_pointer value of object pointed to by
my_pointer (an int here). - In comparison
- int my_int
- my_int name and value of the int
- my_int address of the int
4Define a struct in Turbo C
- Syntax
- struct struct_type
-
- variable-type variable_name
- variable-type variable_name
-
- variable-type variable_name
- //dont forget this
- Declare a variable of user defined struct-type
- struct_type struct_name //after struct_type is
defined
5An Example
- struct student
-
- char name20 //name for student
- long id //id for student
- //dont forget this
- student stud1 //define variable stud1 to be
type student - stud1.name0J
- stud1.name1a
- stud1.name2c
- stud1.name3k
- stud1.id88888888
- cout ltlt The ID of ltlt stud1.name ltlt is ltlt
stud1.idltltendl
6Pointer to struct
- Syntax
- structure_type pointer_name
- An example
- student stud1P //define variable stud1P to be
pointer to student - stud1P-gtname0J //
- stud1P-gtname1a
- stud1P-gtname2c
- stud1P-gtname3k
- stud1P-gtid88888888
- cout ltlt The ID of ltlt stud1P-gtname ltlt is ltlt
stud1P-gtidltltendl - int my_pointer //my_pointer is a pointer to an
integer
7Pointer to struct
- student stud1P //define variable stud1P to be
pointer to student - stud1P the pointer
- stud1P the struct pointed to by the pointer
- (stud1P).id the variable id for the struct
pointed to by the pointer - stud1P-gtid simplified version of (stud1P).id
- the followings are similarly interpreted.
- stud1P-gtname0J
- stud1P-gtname1a
- stud1P-gtname2c
- stud1P-gtname3k
8A Function to get a key and its key number
- struct CharInt char x int y //define a
struct type - CharInt get_key_number () //return type of the
function is CharInt - char a int b
- CharInt tmp
- _AH0x0 //service number 0x00
- geninterrupt(0x16) //interrupt 0x16
- a_AL b_AH //_AL is the key and _AH is the
keynumber - tmp.xa tmp.yb //assign them to a variable of
type CharInt - return tmp //return the value
9Problems caused by Pseudo-variables
Pseudo-variables refer to CPU registers which are
used by other programs which may run at the same
time. One must assign values right before using
them and read values right after obtaining them,
especially when we program in C. Function
geninterrupt has no direct provision for
register manipulation so we have to use
pseudo-variables. Therefore, there is no way to
guarantee that pseudo-variables will retain their
values during a succession of assignment
operations before/after a geninterrupt
invocation.
10More Reliable Access to Registers
Segments registers can be obtained more reliably
by the following approach
- struct SREGS unsigned int es,cs,ss,ds
- void segread (struct SREGS SegReg)
The function segread simply copies the values of
the four segment registers into the corresponding
structure SegReg items.
11Three More Structures
- struct WORDREGS unsigned int ax,bx,cx,dx,si,di,c
flags,flags - struct BYTEREGS unsigned char al,ah,bl,bh,cl,ch,d
l,dh - union REGS struct WORDREGS x struct BYTEREGS h
The item cflags of the WORDREGS structure
reflects the value of the carry flag in item
flags cflag is zero when this flag is not
set. DOS frequently sets this flag to indicate an
error condition.
12An Alternative Function for Invoking Interrupts
- int int86(int N,union REGS in, union REGS out)
- //precondition N is assigned to be the interrupt
number to invoke. The relevant service number and
the data are assigned in the REGS in - //postcondition the interrupt with the specified
service is invoked and the output data are in the
REGSout.
NOTE Function int86 is used to invoke interrupt
when the request for the desired interrupt
service does not use special settings of the
segment registers. Its source code consists of a
call to segread to load the structure
SegReg, and then make a call to invoke
interrupt.
13Input/Output of int86
From structure
To registers
From registers
To structure
- AX,BX,CX,DX,SI,DI,Flag
- DS,ES
14Revised Function for Output a Char
- void output_a_char(int x) //prototype
- //precondition x is the ascii code of a
character - //postcondition the character is output to
screen - void output_a_char(int x) //definition
- REGSin REGSout
- in-gth.ah0x0E
- in-gth.alx
- int86(0x10,in,out)
15Revised Function to get a key and its key number
- struct CharInt char x int y //define a
struct type - CharInt get_key_number () //return type of the
function is CharInt - REGSin REGSout
- CharInt tmp
- REGS.h-gtah0x0 //service number 0x00
- int86(0x16,in,out) //interrupt 0x16
- tmp.xout-gth.al //_AL is the key and _AH is
the keynumber - tmp.yout-gth.ah //assign them to a variable of
type CharInt - return tmp //return the value
16Check any key is hit (for control)
- int key_ready() //return 1 if a key is ready, 0
otherwise - long int x
- REGS in, out //page 358 of chapter12 of the
handout - REGS.h-gtah0x1 //service number 0x00
- int86(0x16, in, out) //interrupt 0x16
- xout.x.flags //get flag register
- if (x(0x40)0) return 1 //if ZF0 a key is
ready - else return 0 //else no key
-
17Write a function to read a key
- char getch() //return char for the key hit
-
- REGS in REGS out
- //page 358 of chapter12 of the handout
- in.h.ah0x0 //service number 0x00
- int86(0x16,in,out) //interrupt 0x16
- return out.h.al //AL is the key
-
- //alternatively, one may simply do
- //char t cin gtgt t return t
18Control by typing keys
- char k
- If (key_ready())
-
- kgetch()
- switch k
-
- case l do_something break
- case r do_something_else break
- case u do_another_thing break
- case d do_different_thing break
-
19More about assignment 2
- Some suggestions are given below
- add a constructor in the class Elevator. This
will reduce the length of the main function and
makes the program clear. - Write a function for pressing buttons. Recall
that we have 24 buttons to be pressed.
20More about assignment 2
- How to open a door?
- Draw a door by drawing 50 vertical lines.
- Use a member variable door to control the door.
- Door is an integer, the value is 25 --3.
- 25-- the door is closed
- 3-- the door is open.
- Open the door
- just draw black lines from 25 to door.
- Close the door
- draw more and more yellow lines.
21More about assignment 2
- When to open the door?
- After your door REALLY stops.
- You have to study the function stop().
- When to close the door
- before the lift moves, i.e., before x.k is
changed. - However to show buttons inside the elevator
- modify the member function selevator() to show
bottons according to array. - if (arrayi1) print a dot/circle
22More about assignment 2
- How to show buttons outside the elevator
- do it according to the two arrays corresponding
to the buttons. Let up and down are the arrays. - A ordinary function can be defined.
- Call the function in main().
- if (upi1) print red square else print
green square - if (downi1) print read circles else print
green circles. - Repeat the above for different is.
23More about assignment 2
- How to test if the lift should be stopped?
- Very hard
- First, just consider the buttons inside the
elevator - two subcases should be considered
- lift is going up (look at x.sign)
- lift is going down
- When buttons outside the elevators are
considered, we have to decide that which elevator
stops. (This is the last step to do.)