Input and Output in Assembly Language

142 CHAPT ER 4 T HE INST RUCT ION SET ARCHIT ECT URE

4.7 Input and Output in Assembly Language

Finally, we come to ways in which an assembly language program can communi- cate with the outside world: input and output IO activities. One way that communication between IO devices and the rest of the machine can be handled is with special instructions, and with a special IO bus reserved for this purpose. An alternative method for interacting with IO devices is through the use of memory mapped IO, in which devices occupy sections of the address space where no ordinary memory exists. Devices are accessed as if they are memory locations, and so there is no need for handling devices with new instructions. As an example of memory mapped IO, consider again the memory map for the ARC, which is illustrated in Figure 4-20. We see a few new regions of memory, A stack frame is created for func_2 as a result of function call at line 04 of program. g A stack frame is created for func_1 as a result of function call at line 19 of program. h func_1 places return value on stack. Line 12 of program. i 2 32 – 4 Free area 2 32 – 4 sp Stack 2 32 – 4 Free area sp func_2 places return value on stack. Line 20 of program. j Program finishes. Stack is restored to its initial configuration. Lines 04 and 05 of program. k 2 32 – 4 Free area 2 32 – 4 Stack 115 sp Stack frame for func_2 Free area sp Stack Stack sp 10 r15 m n 10 r15 m n 10 15 r15 i j func_2 stack frame func_1 stack frame 115 r15 m n 10 Stack Free area Figure 4-19 g-k Continued. CHAPT ER 4 T HE INST RUCT ION SET ARCHIT ECT URE 143 for two add-in video memory modules and for a touchscreen . A touchscreen comes in two forms, photonic and electrical. An illustration of the photonic ver- sion is shown in Figure 4-21. A matrix of beams covers the screen in the horizon- tal and vertical dimensions. If the beams are interrupted by a finger for example then the position is determined by the interrupted beams. In an alternative ver- sion of the touchscreen, the display is covered with a touch sensitive surface. T he user must make contact with the screen in order to register a selection. Reserved for built-in bootstrap and graphics routines Add-in video memory 1 IO space 2 16 Stack pointer System Stack Top of stack Bottom of stack Screen Flash Touchscreen x Touchscreen y 2 24 – 4 2 23 – 4 32 bits Address Data 2 24 – 1 byte Add-in video memory 2 2 17 2 19 Working Memory Unused 2 22 FFFFEC 16 FFFFF0 16 FFFFF4 16 Figure 4-20 Memory map for the ARC, showing memory mapping. LEDs sources Detector User breaks beams Figure 4-21 A user selecting an object on a touchscreen. 144 CHAPT ER 4 T HE INST RUCT ION SET ARCHIT ECT URE T he only real memory occupies the address space between 2 22 and 2 23 – 1. Remember: 2 23 – 4 is the address of the leftmost byte of the highest word in the big-endian format. T he rest of the address space is occupied by other compo- nents. T he address space between 0 and 2 16 – 1 inclusive contains built-in pro- grams for the power-on bootstrap operation and basic graphics routines. T he address space between 2 16 and 2 19 – 1 is used for two add-in video memory modules, which we will study in Problem Figure 4.3. Note that valid informa- tion is available only when the add-in memory modules are physically inserted into the machine. Finally, the address space between 2 23 and 2 24 – 1 is used for IO devices. For this system, the X and Y coordinates that mark the position where a user has made a selection are automatically updated in registers that are placed in the memory map. T he registers are accessed by simply reading from the memory locations where these registers are located. T he “Screen Flash” location causes the screen to flash whenever it is written. Suppose that we would like to write a simple program that flashes the screen whenever the user changes position. T he flowchart in Figure 4-22 illustrates how this might be done. T he X and Y registers are first read, and are then compared with the previous X and Y values. If either position has changed, then the screen is flashed and the previous X and Y values are updated and the process repeats. If neither position has changed, then the process simply repeats. T his is an example of the programmed IO method of accessing a device. See problem 4.3 at the end of the chapter for a more detailed description.

4.8 Case Study: The Java Virtual M achine ISA