Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Computer Organization and Assembly Language CSC-210

Laboratory Manual 3rd Edition 2017/2018

Name:

Number:

ٍ :Section Department:

1 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Table of Contents Lab (1): Computer Anatomy ...... 4 Objectives ...... 4 What is a Computer? ...... 4 System ...... 4 1. Motherboard ...... 5 2. Central Processing Unit (CPU) ...... 6 3. Memory ...... 6 4. Video Adapter ...... 7 5. Operating System ...... 8 6. Connectivity ...... 8 Chassis ...... 9 1. Full Tower ...... 9 2. Mid Tower ...... 9 3. Mini Tower ...... 9 4. Slim Line Case ...... 9 5. Small Form Factor (SFF) Case ...... 9 Cooling System ...... 10 Power Supply Unit ...... 10 Peripherals...... 11 1. Input Devices ...... 11 2. Output Devices ...... 12 3. Input & Output Devices ...... 13 Review Questions ...... 14 Lab (2): PC I/O Ports ...... 16 Objectives ...... 16 Mini-DIN ...... 16 PS/2 ...... 16 Separate Video (S-Video) ...... 16 Universal Serial Bus (USB) ...... 17 Firewire (IEEE 1394) ...... 18 DB (D-subminiature, D-shell) Connectors ...... 19 Registered Jack (RJ) ...... 19 Audio...... 20

1 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

1. Analog ...... 20 2. Digital ...... 20 Digital Visual Interface (DVI) ...... 20 HDMI (High-Definition Multimedia Interface) ...... 21 DisplayPort ...... 21 eSata ...... 22 Summary of Some Bus Speeds ...... 22 Summary of External Connector ...... 23 Review Questions ...... 24 Lab (3): Motherboard ...... 27 Objectives ...... 27 Motherboard Form Factor ...... 27 Processors Sockets ...... 28 Chipset ...... 31 Figure 3-3. Z97 Chipset Block diagram ...... 31 Buses and Expansion slots ...... 31 On-Board Ports and Connectors ...... 33 Review Questions ...... 35 Lab (4): Numbering systems ...... 37 Objectives ...... 37 Review Questions ...... 37 Lab (5): Introduction to Assembly ...... 39 Objectives ...... 39 What is assembly language? ...... 39 Inside the CPU ...... 39 1. General purpose registers ...... 39 2. Segment registers ...... 40 3. Special purpose registers ...... 41 Variables ...... 41 Arrays ...... 41 Constants ...... 42 Review Question: ...... 43 Lab (6): EMU8086 ...... 44 Objectives ...... 44 Using the emulator ...... 45

2 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Review Questions ...... 46 Lab (7): Memory Addressing and Instructions ...... 47 Objectives ...... 47 Memory Addressing Modes ...... 47 Memory Access Instructions...... 48 1. MOV instruction ...... 48 2. XCHG Instruction ...... 49 Review Questions: ...... 50 Lab (8): Arithmetic and Logic Instructions ...... 52 Objectives ...... 52 Arithmetic and Logic Instructions ...... 52 1. First group: ADD, SUB,CMP, AND, TEST, OR, XOR ...... 53 2. Second group: MUL, IMUL, DIV, IDIV ...... 54 3. Third group: INC, DEC, NOT, NEG ...... 55 Review Questions: ...... 55 Lab (9): Program flow control ...... 56 Objectives ...... 56 Unconditional jumps ...... 56 Short Conditional Jumps ...... 57 1. Jump instructions that test single flag ...... 57 2. Jump instructions for signed numbers ...... 58 3. Jump instructions for unsigned numbers ...... 58 Loops...... 59 Review Questions ...... 61 Lab (10): Procedures, Interrupts and Stack ...... 62 Objectives ...... 62 Procedures ...... 62 Interrupts ...... 64 The Stack ...... 66 Review Question ...... 68 Final Project ...... 69

3 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Lab (1): Computer Anatomy Objectives

 Identify the basic components of computer and their working  Explain the importance of various units of a computer

What is a Computer? A computer is a combination of sets of electronic modules united to meet the needs of its user in a symphony of electronic pulses traversing through printed board connectors, integrated circuits, wires and switches. Computers come in all shapes and sizes; some are capable of doing millions of calculations in a fraction of a single second, while others may take longer to do the simplest calculations. Computers generally consist of three major component categories (System, Chassis, and Peripherals) with each category further divides into sub-categories of either essential or optional components.

System Chassis Peripherals

Figure (1.1) Major Computer Components System The System itself is the inner makings of the computer contained within the Chassis. The system consists of a combination of hardware and software coexisting in a symbiotic relationship. That is to use the hardware, a piece of software such as an Operating System (OS) or device driver is needed; vice versa, in order to use a piece of software, existence of compatible hardware is needed. A System must utilize 6 categories of components to classify it as a computer system.

Major Components of a computer system are: 1. Motherboard 2. CPU 3. Memory

4 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

4. Video Adapter 5. Operating System 6. Connectivity

1. Motherboard Even though the processor is the brain of the operation, without a Motherboard (MB) all you will have is a cluster of components, which do not fit each other. Consider the motherboard as the skeleton or a computer where parts, similar to human organs, fit within it.

Figure (1.2) Motherboard Layout

Although different manufacturers build different types of motherboards in a variety of sizes, shapes and integrated features, all motherboards share some basic elements. All motherboards must include an integrated chipset, which establishes the types of components that are compatible with it. All MBs must accommodate at least 1 CPU, capabilities for some type storage device, as well as some common input/output devices such as keyboard and mouse which we’ll discussed later. Every motherboard includes BIOS (Basic Input/output System) chip, which performs system checks at boot-up time of the system. The BIOS also provides the means of communication between the processor and installed peripherals. Most BIOSes allow a limited amount of customization such as power-saving functions and even password protection for the system even before the operating system (OS) loads as a method of protecting your computer from unauthorized users.

5 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

2. Central Processing Unit (CPU) Also known as, “The Processor”, the CPU receives the user’s instructions from the connected input devices, processes it and sends processed data to the appropriate components and output devices. The CPU is where all complex calculations are carried out, as such the faster the CPU the quicker the programs will function and respond. Intel’s Core™ i7 Extreme Edition 10-core processors, AMD’s FX series 8-cores processors, AMD’s Phenom™ X4 Quad-Core are amongst the fastest processors on the market today. If you are looking for blazing fast speed then you certainly want one of these processors. Without the CPU, the whole computer is rendered useless.

Figure (1.3) CPU Top/Bottom view

Like any piece of technology, CPUs come in all shape and sizes. There are 3 major aspects to any CPU: 1. Type: There are different standards for a Processor to select from. Some of the most popular ones are Intel, AMD, and Motorola. 2. Class: Different classes have different minimum and maximum speeds. Some examples of Class are: i7, i5, i3, Celeron, , and Athlon. 1. Speed: Measured in Hertz, it is the speed at which calculations are processed. Examples of values are 4.0GHz and 1.2GHz.

3. Memory Every time an application is opened, the CPU allocates a specified amount of available RAM space to provide faster reaction time to user commands within that application. RAM is a temporary storage space meaning once the application is done using the RAM space, it will be once again available to be used by the next application that needs it. Once RAM’s capacity is reached, the processor redirects its focus to available HDD space also referred to as Virtual Memory (VM). It should be pointed out that HDDs are slower than RAM and as such once your computer resorts to Virtual Memory, applications begin to slow down. As a rule of

6 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017 thumb, more RAM allows you to run more applications simultaneously without slowing your system’s performance.

Figure (1.4) DDR Memory modules

4. Video Adapter The Video adapter is often integrated onto the motherboard or as an add-on video board inserted into motherboard slot connector. It give the system display capabilities. The video adapter turns the processed and translated user input, if any, along with any other information provided by an application and turns it into a video signal to be sent to a display device. Like any data in need of processing, this is done by utilizing the combined usage of system’ CPU and memory. More recent video adapters include onboard processor and memory to increase graphical processes such as those needed in games and applications used by photographers. Integrated video cards come with a blue jack to connect the monitor. Some add-on video adapters also include S-Video, Digital Video Input (DVI) and even Cable TV connectors.

Figure (1.5) Video Display Adapter

7 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

5. Operating System Although not a piece of hardware, without an Operating System (OS) a computer is simply a pile of technology put together like Lego blocks. Selecting an OS is a matter of personal choice; however, there are facts and figures that assist us in the decision on what is best for us or our organization. Having dominated the computing market Microsoft’s Windows Operating Systems can be found virtually anywhere. Apple’s overhaul of its systems to match Microsoft’s hardware needs has made the MacOS a worthy competitor to that of Microsoft Windows OS. Linux is a freeware OS majorly used by scientist and programmers around the world. Linux and other variations of it have brought third world countries in the 21st century and provide capabilities matching those of Microsoft and Apple Operating systems.

Figure (1.6) Major Operating System In the market

6. Connectivity Network connectivity of a computer has become one of the most essential factors as we proceed to integrate computers in all aspects of our lives. All computers poses one or both of 2 methods of connectivity, Wired and Wireless, as means to connect to the Internet, a Local Area Network (LAN), mobile devices or direct (ad-hoc) connection to another computer.

Figure (1.7) Computer Networking

8 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Chassis The Chassis of the computer is the casing that holds and protects all internal components of the computer system from the hazards of the environment around them. The most common system case type is the tower. Depending on the specific number of internal drive bays and the height of the tower, these cases can be further classified into mini- size, mid-size and full-size tower cases. One of the biggest considerations when choosing between case sizes is the number of slots and the number of devices we would like to add to those cases.

1. Full Tower Full-tower cases are generally big with a height that is about or more than 30 inches (more than 76 cm). The number of internal drive bays inside these cases can be between 6 and 10.

2. Mid Tower Another case that might be a step down, would be classified as a mid tower case. Mid-tower cases are the most widely used computer cases. Mid Tower cases are about 18 to 24 (45 to 60 cm) inches high and they usually contain 2 to 4 internal drive bays and a similar number of external bays (for CD/DVD readers and similar).

3. Mini Tower Mini-tower usually has up to 2 or sometimes 3 internal drive bays. Mini-cases normally stand at a height of 12 to 18 inches (30 to 45 cm). Expandability is a problem with these cases

4. Slim Line Case Slim line cases are simply tower cases turned on their sideways. They can hold a monitor on top of the case.

5. Small Form Factor (SFF) Case Small form factor or SFF cases are custom cases that are designed to minimize the spatial volume of a desktop computer. SFFs are available in a variety of sizes and shapes, including shoe boxes, cubes, and book-sized PCs.

9 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Cooling System Manufacturers continue trying to develop new device that take less power to function and generate less heat than the previous generation. However, like any mechanical and electronic device having moving parts and/or using electricity, a computer generates a considerable amount of heat. As such, one of the major parts of any computer case is some type of cooling system.

Spot Cooling (Heat Sink): A primitive form of cooling where a piece of metal specially designed to absorb and disburse heat is attached to a component that generates heat when used.

Air Cooling: Fans are the traditional and most common method to cooling the system as well as a companion to spot cooling of components where spot cooling is not sufficient. Such components include CPU and RAM.

Liquid Cooling: Liquid cooling is the most recent method of cooling a computer. These cooling systems function similar to that of a refrigerator where a coolant circulates through the system by the use of a compressor. Although expensive, it is the most efficient form of cooling system.

Power Supply Unit The power supply unit (PSU) provides the power needed for each component to function. Regardless of what type of computer you are using, the PSU converts AC power from any source to low voltage DC power to be used for the internal components of the computer.

10 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Peripherals A peripheral device is an optional device which when attached to a computer expands its abilities to include specific specialized tasks. There are 3 categories of peripherals, those that assist with input of data and instructions such as a mouse or keyboard, those, which output processed data such as a printer or monitor, and lastly those performing both input and output tasks such as storage devices.

1. Input Devices Input devices are conduits of information transfer from the user to the computer.

Keyboard A keyboard typically has characters engraved or printed on the keys (buttons) and each press of a key typically corresponds to a single written symbol. However, to produce some symbols requires pressing and holding several keys simultaneously or in sequence. While most keyboard keys produce letters, numbers or signs (characters), other keys or simultaneous key presses can produce actions or execute computer commands.

Mouse A computer mouse is a pointing device (hand control) that detects two-dimensional motion relative to a surface. This motion is typically translated into the motion of a pointer on a display, which allows a smooth control of the graphical user interface. Physically, a mouse consists of an object held in one's hand, with one or more buttons.

11 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Scanners An image scanner—often abbreviated to just scanner, is a device that optically scans images, printed text, handwriting or an object and converts it to a digital image. Commonly used in offices are variations of the desktop flatbed scanner where the document is placed on a glass window for scanning. Hand-held scanner is moved by hand.

Modern scanners typically use a charge-coupled device (CCD) or a contact image sensor (CIS) as the image sensor. Digital cameras can be used for the same purposes as dedicated scanners. When compared to a true scanner, a camera image is subject to a degree of distortion, reflections, shadows, low contrast, and blur due to camera shake

Audio Audio devices such as microphones allow the recording of audio such as speech and music for later use in presentations or in creation of a digital compilation media like a Music CD.

2. Output Devices For every input, there is an output to recognize the receipt of an input and to provide a representation of processed data provided via input devices.

Monitors Monitors also referred to as visual display devices are the most common and well-known output devices similar to TVs such that they provide a visual representation of processed data such as images, videos and documents.

Printers Printers are devices that present data shown on a display device on various types of paper in color or black & white by the means of laser, ink or dot matrix technologies.

Audio Audio devices such speakers and headphones act as the computers mouth vocalizing digital audio such as music and video soundtracks and speeches.

12 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

3. Input & Output Devices There exist peripherals, which are used as both input and output devices. Most common of such devices are Removable and Fixed storage devices, such as Disk drives, CDs, and DVDs. Media Storage Storage devices allow us to store our work for future access, might it be from another computer or on the same system. Generally, there are three types of media storage, magnetic, optical and semiconductor. Magnetic drives are such as Floppy and Hard drives store data on magnetic surfaces. Optical drives are such as CRROM, DVD, Blu-ray. Semiconductor drives are flash disks, and memory cards.

Floppy drive Hard drive

DVD Drive Blu-ray Drive

Flash drive SD CARD

13 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Review Questions 1. What are the major components of computer?

2. What is the role of Central Processing Unit?

3. What is the role of BIOS?

4. What are the current types of RAM module in use now?

5. Which video adapter is better integrated onto the motherboard ones or standalone ones? And why?

14 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

6. What is the function of Operating Systems?

7. Name three input devices.

8. Name three output devices.

15 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Lab (2): PC I/O Ports Objectives  Identify the ports computer and their functions.

The purpose of this Lab is to discuss the I/O ports and jacks commonly used with modern PCs. Receptacles or sockets on computers are known as ports or jacks. The part that is inserted into a port is known as a plug. The plug is commonly referred to as the "male end" of the connection, and the port is often referred to as the "female end." Mini-DIN The Mini-DIN standard is a family of small (9 mm) cylindrical connectors with different pin outs for different purposes. The Mini-DIN is similar to the larger, older DIN connector. Both are standards of the Deutsches Institut für Normung, the German standards body. PS/2 The most widely utilized Mini DIN connector utilized in the computer industry is the 6-pin PS/2 keyboard and mouse connector (shown right). Per the PC 99 System Design Guide specification, the PS/2 keyboard port is colored purple and PS/2 mouse port is colored green. These ports also often have a small keyboard icon and mouse icon next to them, respectively, for identification. Separate Video (S-Video) Another mini-DIN connector mentioned in your text is the Separate Video (S-Video) connector. It is an analog video-only connector (colored yellow) similar in size and shape to the PS/2 connector except it has a pin count of four instead of six. It is found on consumer TVs, DVD players, digital TV receivers, DVRs, game consoles, and graphics cards. For the most part, it has been replaced by the DVI and HDMI digital video standards.

One should be careful to properly align the square plastic key on mini-DIN plugs to avoid bending pins when inserting it into a port. Most connectors include an arrow or flat section which is usually oriented to the right or top of the port for proper alignment.

16 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Universal Serial Bus (USB) The use of USB ports is by far the most popular means of attaching peripherals to computers today. Many of the following peripheral devices have USB capability:

Keyboards Mice Printers Scanners Digital cameras Wi-Fi adapters PDAs Smart phones Flash drives External hard drives Floppy drives Optical drives

Most modern computers are equipped with four or more Type A USB ports. USB connections can provide a small amount of power to a device, and devices are hot-swappable - but remember to stop the device, if applicable, before removing it. USB connectors come in a variety of sizes and configurations, as shown below.

Micro and Mini connectors (shown to the left above) were designed for small devices such as cameras and PDAs. A Type A port as found on the back of a computer is shown to the right. There are three versions of USB as shown in the table below. The latest, USB 3.0, will be deployed on motherboards in Q1 2010. The USB 3.0 connector has the same shape as its predecessors, but plugs are longer and the ports are deeper.

17 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

Data Output Max USB Ratification Rate Power Cable Version Date (Mbits/s) (watts) Length (m) Icon 1.0 1996 12 yes 3

2.0 1 2001 480 2.5 5

2 3.0 2008 4800 4.5 3

1. Sustained USB 2.0 throughput is typically no more than 280 Mbit/s. 2. It is predicted that USB 3.0 with its high data rate will very quickly overshaddow and possibly replace other tecnologies such as Firewire and eSATA.

Firewire (IEEE 1394)

Firewire is used with audio-visual peripherals, such as camcorders, because it is somewhat faster than USB 2.0 and can provide more power to the device. For this same reason, it is also sometimes used with external hard drives. The six-pin plug (shown to the left) is commonly used to connect Firewire peripherals to computers. As with USB, Firewire is hot-swappable. The table below outlines some Firewire specifications:

Firewi Ratificatio Data Max re n Date Rate Cable Output Power 400 -Versio IEEE 1394 199 100,(Mbits/s 200, Length4.5 400n - IEEE 2005 100,) 400 200, (m) 4.5 Up to 45 watts, but 7 - 8 1394a 0 400 800 - IEEE 200 786, 3200 10 watts is more typical. 1384b1394b S1600 & 2 0 200 1600, 3200 10 S3200 modes 8 0 Some computers intended for audio/video use have built-in FireWire ports. Windows Visa and prior only supported the 1394a standard. Microsoft has rewritten the 1394 bus driver for Windows 7 to provide support for advanced versions of the1394 standard depicted above.

18 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

DB (D-subminiature, D-shell) Connectors The proper name for DB connectors is actually D-subminiature. They are also called D- shell connecters because they are shaped like the letter D. The DB-25 connector (pictured below) was the first D-shell introduced and subsequently became part of the IEEE-1284 standard. The IEEE-1284 standard also includes the legacy 36 pin Centronics connector that is used to connect to printers (not pictured).

The smaller DE-15, DE-9, and DA-15 subminiature connector (pictured below) were introduced later to provide VGA video, RS-232 serial, and a gaming device interfaces, respectively. The "B" in DB and the "E" in DE and the "A" in DA refer to shell size. The table below summarizes the D-subminiature connectors commonly found on computers.

Name Descri Depiction DB-25 A legacy 25-pin parallelption connector commonly used with printers. The newest printer interface is USB.

DE-15 A 15-pin analog monitor connector referred to in VGA your text as a VESA connector. Also commonly referred to as a VGA (Video Graphics Array) or

"DB-15" connector. DE-9 A nine-pin serial connector used for attaching RS-232 devices such as external modems. Commonly called "DB-9."

DA-15 A legacy 15-pin connector that was commonly found on audio adapters for connecting gaming

devices such as Joysticks. USB and Bluetooth (wireless) are among the newest methods for connecting these devices. Registered Jack (RJ) Two types of Registered Jack (RJ) ports are commonly found with computers: the RJ-11 and the RJ-45. Both are similar in shape with the RJ-45 (pictured right) being slightly larger. The RJ-11 is the standard modular phone connector and is typically found in pairs on modem devices. The RJ-45 is used to connect network adapters to Ethernet networks.

19 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

An RJ-45 port is pictured to the left. There are normally a couple of light- emitting diodes (LEDs) found next to it that provide network connectivity and activity status. The RJ-45 plug is usually connected to various types of eight- wire unshielded twisted pair (UTP) cable that can have a maximum segment length of 100 meters when used in an Ethernet network. A pair of RJ-11 ports, as typically found on modems, is depicted to the right. One port is used to connect the modem to a phone jack and the second port is used to connect to the modem to a phone.

Audio

1. Analog Three analog 3.5 mm (1/8") cylindrical audio ports, as depicted on the left, are typically found on the back of computers for connecting to a microphone, speakers and line in. These ports are colored pink, lime green, and light blue, respectively - and are formally known as tip, ring, and sleeve (TRS) connectors. However, they are also referred to as "mini-jacks." You are probably familiar with microphones and speakers; however, they may not be familiar with a line in connection. A line in connection is used for inputting a signal from a cassette tape recorder or similar sound source 2. Digital Some advanced audio adapters may also have an RCA port (depicted right) to support Sony/Phillips digital interface format (S/PDIF) audio output. This port, color-coded orange, can be used to connect the computer to a home theater receiver equipped with surround- sound speakers.

Digital Visual Interface (DVI) DVI is a video interface standard used with digital display devices such as flat screen monitors. A typical DVI port (colored white per the PC 99 Design Guide) is shown to the left.

DVI is compatible with the High-Definition Multimedia Interface (HDMI) standard. However, DVI only supports video whereas HDMI supports both video and audio.

20 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition/2017

HDMI (High-Definition Multimedia Interface) HDMI, available since 2003, supports both audio and video and is now the de facto standard for digital (flat screen) TVs, including HDTV. It is also widely available on digital (flat screen) PC monitors. Other uses for HDMI include Blue-ray disc players which were introduced in 2006. HDMI complements DVI in that, with an appropriate adapter, HDMI video can be ported to a DVI interface.

There are four types of HDMI connectors as outlined in the table below. As with USB, the Type A connector is the one primarily used to connect electronic devices to desktop computers.

Type Size Pins Comments A 14 mm × 4.55 mm 19 Standard version B 21.2 mm × 4.45 mm 29 Future use C 10.42 mm × 2.42 mm 19 Mini version D 6.4 mm x 2.8 mm 19 Micro version

DisplayPort

Consumer electronics and PC manufacturers incorporating HDMI into their devices must pay a royalty fee of $10,000 per year plus four cents per unit. In 2008, a new audio/visual interface standard named DisplayPort became available that is royalty free and provides a superset of features found in DVI and the core set of features found in HDMI. It was created by Video Electronics Standards Association (VESA) as a more up-to-date alternative to DVI and a cheaper alternative to HDMI. It is targeted primarily towards PC manufactures as a means of connect computers to monitors, and computers to home-theater system. Both Dell and Apple are currently shipping PCs and monitors equipped with Display Port - often as a replacement for DVI.

Display Port video has several technical advantages over DVI video and uses a much smaller connector that doesn't require thumbscrews. Display Port connector pins don't run the risk of bending if improperly handled as is the case with DVI. Further, the DisplayPort connector is easier to connect when guided only by touch. Due to its enhanced capability and connector design, DisplayPort may eventually replace DVI in the PC industry. It may also make inroads into HDMI - primarily due to cost.

21 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018 eSata The eSATA connector (shown right) is almost exclusively used to connect external SATA drives to computers. The SATA standard's throughput of 3000 Mbits/s is considerably faster than that the approximate 400 Mbits/s throughput of USB 2.0 and Firewire 400 IEEE 1394 - however, it does not provide power. It may be overshadowed by the upcoming deployment of USB 3.0 which has a throughput of 4800 MBits/s and can provide power. Summary of Some Bus Speeds

Bus Throughput (Mbps) USB 2.0 480 (theoretical) Firewire 400 400 Firewire 800 786 Firewire 1600 1600 Firewire 3200 3200 eSATA 3000 USB 3.0 4800 (theoretical)

22 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Summary of External Connector

Hot Connection Swap- Provides Connector Usage Color pable Power Depiction PS/2 Keyboard Purple No No Mini-DIN Mouse Green No No

Numerous Serial USB Black Yes Yes devices

Audio-visual Firewire peripherals; Grey Yes Yes IEEE 1394 external hard drives

DB-25 IEEE 1284 Printers Fuschia No No Parallel port

DE-15 Monitors Blue No No VGA port

DE-9 RS-232 serial External modems Teal No No port

DA-15 Gaming devices Gold No No Game Port

RJ-45 Network adapters -- Yes Possibly

RJ-11 Modems -- Yes No

Microphone Pink TRS Speaker Lime Green Yes No Mini Jacks Line in Light Blue S/PDIF Digital audio Orange Yes No

DVI Digital video White Yes No

Digital HDMI -- Yes No Audio/Video Digital DisplayPort -- Yes No Audio/Video

23 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Questions 1. What are two ways that USB version 2.0 ports can be added to a computer?

A. Implement a USB hub B. Add a USB 1.1 expansion card C. Add a USB 2.0 expansion card D. Use an Ethernet hub 2. A serial port can hook up devices such as external modems and label printers. What is this port usually called?

A. SCSI port B. COM port C. PS/2 ports D. Parallel port 3. Which of the following devices can be used for a printer port, scanner, or removable media?

A. PS/2 port B. Parallel port C. NIC card D. I/O port 4. Which device is known as IEEE 1394?

A. USB B. Parallel port C. FireWire D. PS/2 5. Some desktop systems and many of the older laptop and portable systems include a port to connect a mouse or keyboard. What is this port called?

A. USB B. FireWire C. BIOS D. PS/2 6. What is the standard size of the audio mini-jack used by sound cards?

A. 1 1/2 inch B. 1/8 inch C. 2 1/2 inch D. 1 inch 7. There are currently three standards for USB ports. What are they? (Choose three.)

A. USB 1.1 B. USB 3.0 C. USB 1.0 D. USB 2.0 8. If you run out of USB ports and need more, which of the following devices are available? (Choose all that apply.)

24 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

A. Motherboard connectors B. USB hubs C. Add-on cards D. Extra PCI slots 9. Which device still remains the primary method used to enter data and send commands to the computer?

A. Mouse B. Gamepad C. Stylus D. Keyboard 10. Which of the following is used to transfer data into a computer by pressing on screen icons?

A. Touch screen monitors B. CRT monitors C. LCD monitors D. Serial ports 11. Which of the following are considered multimedia devices? (Choose all that apply.)

A. Webcam B. Sound card C. Microphone D. All of these options are correct. 12. You are in the process of installing a new keyboard to a new PC. Which of the following is the most common type of connector to use?

A. Serial connector B. PS/2 connector C. USB connector D. IEEE1394 connector 13. What is the major difference between a laser printer and an LED printer? (Choose all that apply.)

A. LED printers use an LED array to perform the transfer of images. B. LED printers use an LED drum. C. Laser printers are of better print quality. D. Laser printers use a laser to transfer the image to the drum.

25 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

14. Most inkjet, laser, and thermal printers use this interface to connect a printer to a computer. (Choose two.)

A. RJ-45 B. USB C. Parallel D. LED 15. What are three major types of display devices that are in use in today's industry? (Choose all that apply.)

A. CRT monitors B. LCD monitors C. Data projectors D. USB monitors 16. Identify three types of video connectors.

A. VGA type B. DMI type C. HDMI type D. USB type

26 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (3): Motherboard

Objectives  Identify the different types and features of motherboards.  Understand how firmware on the motherboard controls a PC  Learn to install, configure, and maintain a motherboard

A motherboard is the most complicated component in a computer. When you put together a computer from parts, generally you start with deciding on which processor and motherboard you will use. Everything else follows those decisions. Take a look at the details of Figure 3-1, which shows a motherboard designed with gamers in mind. We’ll also look at how configuration information is stored on a motherboard and the best strategies to use when selecting a motherboard.

Figure 3-1 ASUS Z97 ATX-Motherboard details

Motherboard Form Factor A motherboard form factor determines the size of the board and its features that make it compatible with power supplies, cases, processors, and expansion cards. The most popular

27 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

motherboard form factors are ATX, Mini ATX, EATX, MicroATX, and FlexATX, ATX motherboards have been around for a long time and have seen many improvements. Figure 3-2 shows the differences among ATX family form factor.

Figure 3-2. ATX family Form Factors

Processors Sockets Another important feature of a motherboard is the processor socket. This socket and the chipset determine which processors a board can support. A socket will hold either an Intel or AMD processor. Some older processors were installed on the motherboard in a long narrow slot, but all processors sold today use sockets. Table 3-1 lists the sockets currently used by Intel processors for desktop systems. Earlier Pentiums used a (PGA) socket, with pins aligned in uniform rows around the socket. Later sockets used a staggered pin grid array (SPGA), with pins staggered over the socket to squeeze more pins into a small space. Small pins can easily be bent as the processor is installed in the socket. Later Intel sockets use a (LGA) that uses lands rather than pins. The first LGA socket is the LGA775 socket. It has 775 lands and is shown with the socket lever and top open in Figure 3-4. The lands look like tiny pads that the pins on the processor contact. The latest Intel socket is the LGA1366 socket. It’s called a land grid array socket, but the lands in the socket are actually more like pins that connect with lands on the bottom of the processor. Figure 3-5 shows an LGA1366 socket with the load plate and load lever lifted so that the socket is open and ready to receive the processor. PGA, SPGA, and LGA sockets are all square or nearly square. So that even force is applied when inserting the processor in the socket, all current processor sockets have a lever on the side of the socket. These sockets are called zero insertion force (ZIF) sockets, and this lever is used to lift the processor up and out of the socket. Push the lever down and the processor moves into its pin or land connectors with equal force over the entire housing. With this method, you can easily remove and replace the processor if necessary. However, know that processors generally

28 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018 should not be removed or replaced repeatedly because this can damage the delicate pins or socket holes.

CPU Computer Bus ies Pin count Pin transfers Package Socket Notes name Year Year type rted suppo

& clock famil

Socket 478/ 2000 Intel Pentium 4 Desktop PGA 478 100– Socket N Intel Celeron 200 MHz Intel Pentium 4 EE 400– Intel Pentium 4 M 800 MT/s

Socket 495 2000 Intel Celeron Notebook PGA 495 66–133MHz Intel Pentium III

[8] 2003 Intel Pentium M Notebook PGA 479 100– Intel Celeron M 133 MHz 400– 533 MT/s

rPGA 988A/ 2008 i7 (600, Notebook rPGA 988 2.5 GT/s, 700, 800, 900 4.8 GT/s series) Intel Core i5 (400, 500 series) Intel Core i3 (300 series) Intel Pentium (P60 00 series) Intel Celeron (P40 00 series)

Socket 441 2008 Intel Atom Sub- PGA 441 400– notebook 667 MHz

LGA 1156/ 2009 Intel Core i7 (800 Desktop LGA 1156 2.5 GT/s DMI bus is a Socket H series) (perhaps Intel Core i5 (700, modified) 600 series) PCIe x4 v1.1 Intel Core i3 (500 interface series) Intel Xeon (X3400, L3400 series) Intel Pentium (G60 00 series) Intel Celeron (G10 00 series)

rPGA 988B/ 2011 Intel Core i7 (2000, Notebook rPGA 988 2.5 GT/s, 3000 series) 4.8 GT/s Intel Core i5 (2000, 3000 series) Intel Core i3 (2000, 3000 series)

29 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

CPU Computer Bus ies Pin count Pin transfers Package Socket Notes name Year Year type rted suppo

& clock famil

LGA 1150/ 2013.06.03 Intel Haswell Desktop LGA 1150 ? used for Intel's Socket H3 2014.05.11 Intel Haswell 4th generation (2015.06.0 Refresh (Haswell/Hasw 2) Intel Broadwell ell Refresh) processors

Socket G3 2013/Q2 Intel Haswell Notebook rPGA 946 ? Intel Broadwell

LGA 1151 2015 Intel Skylake Desktop LGA 1151 ? used for Intel's Intel Server 6th generation (Skylake) and 7th generation (Kaby Lake) processors

LGA 2066/ 2017 Intel Skylake-X Desktop LGA 2066 ? Used for Intel's Socket R4 Intel Kaby Lake-X Server 7th generation (Skylake-X & Kaby Lake-X) series of Core- X processors Table 3-1. Intel Sockets

30 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Chipset Each socket is supported by the chipset. The chipset and associated components provide peripheral connectivity to the CPU. Because the chipset defines the types and limits of most connections between the CPU and peripherals, it may be the most important consideration in a motherboard. A chipset is basically composed of a Northbridge and a Southbridge, although the last few generations of Intel CPUs and recent AMD APUs integrate Northbridge functions within the CPU die. The interfaces requiring the greatest performance are integrated onto the Northbridge, and slower interfaces connect through the Southbridge. Since they are the oldest chipsets available today.

Figure 3-3. Intel Z97 Chipset Block diagram

Buses and Expansion slots Look on the bottom of the motherboard, and you see a maze of circuits that make up a bus. These embedded wires carry four kinds of cargo:  Electrical power. Chips on the motherboard require power to function. These chips tap into a bus’s power lines and draw what they need.  Control signals. Some wires on a bus carry control signals that coordinate all the activity.  Memory addresses. Components pass memory addresses to one another, telling each other where to access data or instructions. The number of wires that make up the memory address lines of the bus determines how many bits can be used for a memory address. The number of wires thus limits the amount of memory the bus can address.

31 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

 Data. Data passes over a bus in a group of wires, just as memory addresses do. The number of lines in the bus used to pass data determines how much data can be passed in parallel at one time. The number of lines depends on the type of processor and determines the number of bits in the data path. (Remember that a data path is the part of the bus on which the data is placed; it can be 8, 16, 32, 64, or more bits wide.) The system clock keeps the beat for components. If a component on the motherboard works by the beat, or clock cycle, then it is synchronized, or in sync, with the processor. For example, the back-side bus of the Pentium works at half the speed of the processor. This means that the processor does something on each clock cycle, but the back-side bus is doing something on every other clock cycle. Some components don’t attempt to keep in sync with the processor, even to work at one-half or one- third of clock cycles. These components work asynchronously with the processor. They might work at a rate determined by the system clock or by another crystal on or off the mother- board. Either way, the frequency is much slower than the processor’s and not in sync with it. If the processor requests something from one of these devices and the device is not ready, the device issues a wait state, which is a command to the processor to wait for slower devices to catch up. Data Path Bus Bus Type in Bits Address Lines Bus Frequency Throughput System bus Local 64 32 or 64 Up to 1600 MHz Up to 3.2 GB/sec PCI Express Local video Serial with Up to 32 lanes 2.5 GHz Up to 500 MB/sec per Version 2 and local I/O up to 32 lanes lane in each direction PCI Express Local video Serial with Up to 16 lanes 1.25 GHz Up to 250 MB/sec per Version 1.1 and local I/O up to 16 lanes lane in each direction PCI Express Local video Serial with Up to 16 lanes 1.25 GHz Up to 250 MB/sec per Version 1 and local I/O up to 16 lanes lane in each direction PCI-X Local I/O 64 32 66, 133, 266, Up to 8.5 GB/sec or 533 MHz PCI Local I/O 32 or 64 32 or 64 33, 66 MHz 133, 266, or 532 MB/sec AGP 1x, 2x, Local video 32 NA 66, 75, 266 MB/sec to 3x, 4x, 8x 100 MHz 2.1 GB/sec FireWire 400 Local I/O 1 Serial NA Up to 3.2 Gbps and 800 or expansion (gigabits per second) USB 1.1, 2.0, Expansion 1 Serial 3 MHz 12 or 480 Mbps and 3.0 (megabits per second) or 5.0 Gbps (gigabits per second)

Table 3-2 Buses list by throughput

Table 3-2 lists the various buses used on motherboards today, in order of throughput speed from fastest to slowest. (Throughput is sometimes called bandwidth.) Looking at the second column of Table 3-2, you can see that a bus is called an expansion bus, local bus, local I/O bus, or local video bus. A bus that does not run in sync with the system clock is called an expansion bus and always connects to the slow end of the chipset, the South Bridge. Most buses today are local buses, meaning they run in sync with the system clock. If a local bus connects to the slower I/O controller hub or South Bridge of the chipset, it is called a local I/O bus. Because the video card needs to run at a faster rate than other expansion cards, this one slot always connects to the faster end of the chipset,

32 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018 the North Bridge. This video slot can be either an AGP slot or a PCI Express x16 slot, and the bus is called a local video bus.

Figure 3-4. With PCI Version 2.x, there are four possible types of expansion slots and six differently configured PCI expansion cards to use these slots

On-Board Ports and Connectors

In addition to expansion slots, a motherboard might also have several on-board ports and internal connectors. Ports coming directly off the motherboard are called on-board ports or integrated components. Almost all motherboards have two or more USB ports and sound ports. Boards might also offer a network port, modem port, FireWire (IEEE 1394) port, video port, keyboard port, mouse port, parallel port, serial port, one or more eSATA ports (for external SATA hard drives), and a port for a wireless antenna.

33 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Rear panel connectors

1. Optical S/PDIF Out port 7. Mini DisplayPort

2. HDMI port 8. Wi-Fi 802.11 a/b/g/n/ac, Bluetooth V4.0*

3. USB 2.0 ports 7-10 9. USB 3.0 ports E56, support ASUS USB 3.0 Boost Turbo Mode.

® USB 3.0 ports 56, support ASUS USB 4. Intel LAN port (LAN2)** 10. 3.0 Boost Turbo Mode. ® USB 3.0 ports 34, Bottom port supports 5. Intel LAN port (LAN1)** 11. USB BIOS Flashback and USB Charger+.

6. DisplayPort 12. Audio I/O ports***

Figure 3-5. A motherboard provides ports for common I/O devices

When you purchase a motherboard, the package includes an I/O shield, which is the plate that you install in the computer case that provides holes for these I/O ports. The I/O shield is the size designed for the case’s form factor and the holes in the shield are positioned for the motherboard ports (see Figure 3-6). When you first install a motherboard, you might need to install the drivers that come on the CD bundled with the board before some of the motherboard ports will work.

Figure 3-6. The I/O shield fits the motherboard ports to the computer case

34 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Questions 1. Write the parts names of motherboard?

35 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

2. What are the primary functions of the motherboard and list out their various form factors?

3. List out some of the on-board supported features of modern motherboards.

4. What are the important components of a motherboard?

5. What are the differences between the northbridge and southbridge?

6. What is the role of the BIOS and CMOS in the boot process?

7. What is the CPU socket?

8. What is the slot used by graphic cards?

36 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (4): Numbering systems

Objectives  Review and explain the importance types of numbering systems types.  Explain the basic operations performed on numbers. See Appendix A: Numbering Systems Handouts Review Questions 1. Find the 1st and 2nd complement of the following decimal values? a) 154 b) – 413

2. What is the 3-digit 10’s complement of 247? Answer:

3. What is the 3-digit 10’s complement of 17? Answer:

4. 777 is a 10’s complement representation of what decimal value? Answer:

5. What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer:

6. 1100011 is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer: 7. Add 010011112 to 001000112 using signed-magnitude arithmetic.

8. Add 100100112 (–19) to 000011012 (+13) using signed-magnitude arithmetic.

9. Subtract 010011112 to 011000112 using signed-magnitude arithmetic.

10. Subtract 011000112 (99) from 010011112 (79) using signed magnitude arithmetic.

11. Subtract 100110002 (–24) from 101010112 (–43) using signed-magnitude arithmetic.

12. Express 2310 and 910 in 8-bit binary one's complement form.

13. Add 2310 to –910 using one's complement arithmetic.

14. Express 2310, –2310, and –910 in 8-bit binary two's complement forms.

37 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

15. Add 910 to –2310 using two's complement arithmetic.

16. The 2s complement of (01010)2 is

17. The 2s complement of (0.0010)2 is

18. The 10s complement of (4887)10 is

19. The 10s complement of (48.87)10 is

38 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (5): Introduction to Assembly Objectives  Identify computer registers and there functions  Identify the variable declaration and allocation in assembly. What is assembly language? Assembly language is a low level programming language. You need to get some knowledge about computer structure in order to understand anything. The simple computer model as I see it:

The system bus (shown in yellow) connects the various components of a computer. The CPU is where most of computations occur. RAM is a place to where the programs are loaded in order to be executed.

Inside the CPU

1. General purpose registers

8086 CPU has 8 general purpose registers; each register has its own name: AX - the accumulator register (divided into AH / AL). BX - the base address register (divided into BH / BL).

39 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

CX - the Count register (divided into CH / CL). DX - the data register (divided into DH / DL). SI - Source Index register. DI - Destination Index register. BP - Base Pointer register. SP - Stack Pointer register.

Despite the name of a register, it's the programmer who determines the usage for each general purpose register. The main purpose of a register is to keep a number (variable). The size of the above registers is 16 bit, it's something like: 0011000000111001b (in binary form), or 12345 in decimal (human) form. Four general purpose registers (AX, BX, CX, DX) are made of two separate 8 bit registers, for example if AX= 0011000000111001b, then AH=00110000b and AL=00111001b. Therefore, when you modify any of the 8 bit registers 16 bit register is also updated, and vice-versa. The same is for other 3 registers, "H" is for high and "L" is for low part. Registers are much faster than memory because they are located inside the CPU. Accessing a memory location requires the use of a system bus, so it takes much longer. Accessing data in a register usually takes no time. Therefore, you should try to keep variables in the registers. Register sets are very small and most registers have special purposes which limit their use as variables, but they are still an excellent place to store temporary data of calculations.

2. Segment registers CS – Code Segment register points at the segment containing the current program. DS – Data Segment register generally points at segment where variables are defined. ES - Extra segment register, it's up to a coder to define its usage. SS – Stack Segment register points at the segment containing the stack. Although it is possible to store any data in the segment registers, this is never a good idea. The segment registers have a very special purpose - pointing at accessible blocks of memory. Segment registers work together with general purpose register to access any memory value. For example if we would like to access memory at the physical address 12345h (hexadecimal), we should set the DS = 1230h and SI = 0045h. This is good, since this way we can access much more memory than with a single register that is limited to 16 bit values. CPU makes a calculation of physical address by multiplying the segment register by 10h and adding general purpose register to it (1230h * 10h + 45h = 12345h):

The address formed with 2 registers is called a logical address. By default BX, SI and DI registers work with DS segment register; BP and SP work with SS segment register. Other general purpose registers cannot form a logical address! Also, although BX can form a logical address, BH and BL cannot.

40 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

3. Special purpose registers IP - the Instruction Pointer. Flags register - determines the current state of the microprocessor. IP register always works together with CS segment register and it points to currently executing instruction. Flags register is modified automatically by CPU after mathematical operations, this allows to determine the type of the result, and to determine conditions to transfer control to other parts of the program. Generally you cannot access these registers directly, the way you can access AX and other general registers, but it is possible to change values of system registers using some tricks that you will learn a little bit later.

Variables Variable is a memory location. For a programmer it is much easier to have some value be kept in a variable named "var1" then at the address 5A73:235B, especially when you have 10 or more variables. Our compiler supports two types of variables: BYTE and WORD.

Syntax for a variable declaration: name DB value name DW value

DB - stays for Define Byte. DW - stays for Define Word. name - can be any letter or digit combination, though it should start with a letter. It's possible to declare unnamed variables by not specifying the name (this variable will have an address but no name). value - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "?" symbol for variables that are not initialized.

Arrays Arrays can be seen as chains of variables. A text string is an example of a byte array, each character is presented as an ASCII code value (0..255). Here are some array definition examples: a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h b DB 'Hello', 0 b is an exact copy of the a array, when compiler sees a string inside quotes it automatically converts it to set of bytes. This chart shows a part of the memory where these arrays are declared:

41 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

You can access the value of any element in array using square brackets, for example: MOV AL, a[3]

You can also use any of the memory index registers BX, SI, DI, BP, for example: MOV SI, 3 MOV AL, a[SI] If you need to declare a large array you can use DUP operator. The syntax for DUP: number DUP ( value(s) ) number - number of duplicate to make (any constant value). value - expression that DUP will duplicate.

For example: C DB 5 DUP(9) This is an alternative way of declaring: c DB 9, 9, 9, 9, 9 One more example: d DB 5 DUP(1, 2) is an alternative way of declaring: d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2 Constants Constants are just like variables, but they exist only until your program is compiled (assembled). After definition of a constant its value cannot be changed. To define constants EQU directive is used:

name EQU < any expression >

For example:

k EQU 5

MOV AX, k

42 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Question:

1. List types of data registers?

1- 2- 3-

2. List types of Sections Registers?

1- 2- 3-

3. What are the Differences between AH & AX? ------

43 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (6): EMU8086 Objectives  Explain Loading the DEBUG Program. 1. Start Emu8086 by selecting its icon from the start menu, or by running “Emu8086.exe”.

After the application starts, the following popup window will be displayed. Select "Code Samples" to see examples or select “Continue” it you will write a new program. 2.

3. From the tool bar select “New” icon.

4. From the following dialog box select “COM template”.

5. A new document file with COM template appears in the application window. Write down your program. 6. Click [Compile] then [Emulate] button (or press F5 hot key). 7. Click [Single Step] button (or press F8 hot key), and watch how the code is being executed.

44 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Using the emulator If you want to load your code into the emulator, just click "Emulate" button. But you can also use emulator to load executables even if you don't have the original source code. Select Show emulator from the Emulator menu.

Try loading files from "MyBuild" folder. If there are no files in "MyBuild" folder return to source editor, select Examples from File menu, load any sample, compile it and then load into the emulator:

[Single Step] button executes instructions one by one stopping after each instruction. [Run] button executes instructions one by one with delay set by step delay between instructions. Double click on register text-boxes opens "Extended Viewer" window with value of that register converted to all possible forms. You can modify the value of the register directly in this window. Double click on memory list item opens "Extended Viewer" with WORD value loaded from memory list at selected location. Less significant byte is at lower address: LOW BYTE is loaded from selected position and HIGH BYTE from next memory address. You can modify the value of the memory word directly in the "Extended Viewer" window. You can modify the values of registers on runtime by typing over the existing values. [Flags] button allows you to view and modify flags on runtime.

45 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Questions

1. Run the “Hello.asm” sample program and use the REGISTER command to display the current contents of all of the 80x86's internal registers. List the initial values held in CS, DS, and SS. CS = ______, DS = ______, SS =______

2. Write the following program that demonstrates the use of MOV instruction and trace it.

#MAKE_COM# ; instruct compiler to make COM file. ORG 100h ; directive required for a COM program. MOV AX, 0B800h ; set AX to hexadecimal value of B800h. MOV DS, AX ; copy value of AX to DS. MOV CL, 'A' ; set CL to ASCII code of 'A', it is 41h. MOV CH, 01011111b ; set CH to binary value. MOV BX, 15Eh ; set BX to 15Eh. MOV [BX], CX ; copy contents of CX to memory at B800:015E RET ; returns to operating system.

Step AX BX CX CH CL DS B800:015E 1

2

3

4

5

6

46 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (7): Memory Addressing and Instructions Objectives

 Understand Memory Addressing Modes  Learn how to use Memory access Instructions Memory Addressing Modes To access memory we can use these four registers: BX, SI, DI, BP, combining these registers inside [] symbols, we can get different memory locations. these combinations are supported (addressing modes):

[BX + SI] [SI] [BX + SI + d8] [BX + DI] [DI] [BX + DI + d8] [BP + SI] d16 (variable offset only) [BP + SI + d8] [BP + DI] [BX] [BP + DI + d8]

[SI + d8] [BX + SI + d16] [SI + d16] [DI + d8] [BX + DI + d16] [DI + d16] [BP + d8] [BP + SI + d16] [BP + d16] [BX + d8] [BP + DI + d16] [BX + d16]

d8 - stays for 8 bit signed immediate displacement (for example: 22, 55h, -1, etc...) d16 - stays for 16 bit signed immediate displacement (for example: 300, 5517h, -259, etc...). Displacement can be an immediate value or offset of a variable, or even both. if there are several values, assembler evaluates all values and calculates a single immediate value.. Displacement can be inside or outside of the [ ] symbols, assembler generates the same machine code for both ways. Displacement is a signed value, so it can be either positive or negative. Generally the compiler takes care about difference between d8 and d16, and generates the required machine code. Example: Let's assume that DS = 100, BX = 30, SI = 70. The following addressing mode: [BX + SI] + 25 is calculated by processor to this physical address: 100 * 16 + 30 + 70 + 25 = 1725.

By default DS segment register is used for all modes except those with BP register, for these SS segment register is used. There is an easy way to remember all those possible combinations using this chart:

You can form all valid combinations by taking only one item from each column or skipping the column by not taking anything from it. As you see BX and BP never go together. SI and DI also don't go together. Here are examples of valid addressing modes: [BX+5], [BX+SI], [DI+BX-4]

47 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

The value in segment register (CS, DS, SS, ES) is called a segment, and the value in purpose register (BX, SI, DI, BP) is called an offset. When DS contains value 1234h and SI contains the value 7890h it can be also recorded as 1234:7890. The physical address will be 1234h * 10h + 7890h = 19BD0h. If zero is added to a decimal number it is multiplied by 10, however 10h = 16, so if zero is added to a hexadecimal value, it is multiplied by 16, for example: 7h = 7 70h = 112

In order to say the compiler about data type, these prefixes should be used:  byte ptr - for byte.  word ptr - for word (two bytes). For example:

byte ptr [BX] ; byte access. or word ptr [BX] ; word access.

In certain cases the assembler can calculate the data type automatically.

Memory Access Instructions

1. MOV instruction

 Copies the second operand (source) to the first operand (destination).  Source operand can be an immediate value, general-purpose register or memory location.  Destination register can be a general-purpose register, or memory location.  Both operands must be the same size, which can be a byte or a word.

These types of operands are supported: MOV REG, memory MOV memory, REG MOV REG, REG MOV memory, immediate MOV REG, immediate

REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP

Memory: [BX], [BX+SI+7], variable, etc...

Immediate: 5, -24, 3Fh, 10001101b, etc...

48 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

For segment registers only these types of MOV are supported: MOV SREG, memory MOV memory, SREG MOV REG, SREG MOV SREG, REG

SREG: DS, ES, SS, and only as second operand: CS. REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. Memory: [BX], [BX+SI+7], variable, etc...

The MOV instruction cannot be used to set the value of the CS and IP registers.

Here is a short program that demonstrates the use of MOV instruction:

2. XCHG Instruction

Mnemonic Description XCHG AX,r16 Exchange r16 with AX. XCHG r16,AX Exchange AX with r16. XCHG EAX, r32 Exchange r32 with EAX. XCHG r32, EAX Exchange EAX with r32. XCHG r/m8, r8 Exchange r8 (byte register) with byte from r/m8. XCHG r8, r/m8 Exchange byte from r/m8 with r8 (byte register). XCHG r/m16, r16 Exchange r16 with word from r/m16. XCHG r16, r/m16 Exchange word from r/m16 with r16. XCHG r/m32, r32 Exchange r32 with doubleword from r/m32. XCHG r32, r/m32 Exchange doubleword from r/m32 with r32.

XCHG exchanges the contents of the destination (first) and source (second) operands. The operands can be two general-purpose registers or a register and a memory location. If a memory operand is referenced, the processor's locking protocol is automatically implemented for the duration of the exchange operation.

49 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Questions: 1. Write the syntax of MOV, XCHG?

2. Explain the following statements? 1- MOV AX, zword 2- MOV AL, 'A'

3. Explain the following statements? 1- XCHG AH, BL 2- XCHG AX, ZWORD

4. Save the Value of A in B ž

5. Find the error and rewrite the statements? 1-MOV DS, 1000 2- MOV DS, ES

6. Implement the Assembly Language Program to find sum of numbers in the array. Use emu8086 emulator.

ALGORITHM: 1. Start. 2. Initialize counter = 10. 3. Initialize array pointer. 4. Sum = 0. 5. Get the array element pointed by array pointer. 6. Add array element in the Sum. 7. Increment array pointer decrement counter. 8. Repeat steps 4, 5 & 6 until counter = 0. 9. Display Sum. 10. Stop.

50 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

PROGRAM: include “emu8086.inc” ORG 100 .data ARRAY DB 12H, 24H, 26H, 63H, 25H, 86H, 2FH, 33H, 10H, 35H SUM DW 0 .code START: MOV AX, @DATA MOV DS, AX MOV CL, 10 XOR DI, DI MOV BX, OFFSET ARRAY BACK: MOV AL, [BX+DI] MOV AH, 00H MOV SUM, AX INC DI DEC CL JNZ BACK ; print out the number in ax CALL print_num ; using procedure RET END

51 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (8): Arithmetic and Logic Instructions Objectives  Identify Arithmetic Instructions - ADD,SUB, CMP, INC, DEC, NOT, NEG  Identify Assembly basic Instructions - MUL, DIV, IMUL, IDIV Arithmetic and Logic Instructions Most Arithmetic and Logic Instructions affect the processor status register (or Flags):

As you may see there are 16 bits in this register, each bit is called a flag and can take a value of 1 or 0.

 Carry Flag (CF) - this flag is set to 1 when there is an unsigned overflow. For example when you add bytes 255 + 1 (result is not in range 0...255). When there is no overflow this flag is set to 0.

 Zero Flag (ZF) - set to 1 when result is zero. For none zero result this flag is set to 0.

 Sign Flag (SF) - set to 1 when result is negative. When result is positive it is set to 0. Actually this flag take the value of the most significant bit.

 Overflow Flag (OF) - set to 1 when there is a signed overflow. For example, when you add bytes 100 + 50 (result is not in range -128...127).

 Parity Flag (PF) - this flag is set to 1 when there is even number of one bits in result, and to 0 when there is odd number of one bits. Even if result is a word only 8 low bits are analyzed!

 Auxiliary Flag (AF) - set to 1 when there is an unsigned overflow for low nibble (4 bits).

 Interrupt enable Flag (IF) - when this flag is set to 1 CPU reacts to interrupts from external devices.

 Direction Flag (DF) - this flag is used by some instructions to process data chains, when this flag is set to 0 - the processing is done forward, when this flag is set to 1 the processing is done backward.

52 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

There are 3 groups of instructions.

1. First group: ADD, SUB,CMP, AND, TEST, OR, XOR These types of operands are supported:  REG, memory  memory, REG  REG, REG  memory, immediate  REG, immediate  REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.

 memory: [BX], [BX+SI+7], variable, etc...

 immediate: 5, -24, 3Fh, 10001101b, etc... After operation between operands, result is always stored in first operand. CMP and TEST instructions affect flags only and do not store a result (these instructions are used to make decisions during program execution). These instructions affect these flags only: CF, ZF, SF, OF, PF, AF.  ADD - add second operand to first.  SUB - Subtract second operand to first.  CMP - Subtract second operand from first for flags only.  AND - Logical AND between all bits of two operands. These rules apply: 1 AND 1 = 1 1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0

As you see we get 1 only when both bits are 1.

 TEST - The same as AND but for flags only.

 OR - Logical OR between all bits of two operands. These rules apply: 1 OR 1 = 1 1 OR 0 = 1 0 OR 1 = 1 0 OR 0 = 0

As you see we get 1 every time when at least one of the bits is 1.

 XOR - Logical XOR (exclusive OR) between all bits of two operands. These rules apply: 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0

As you see we get 1 every time when bits are different from each other.

53 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

2. Second group: MUL, IMUL, DIV, IDIV

These types of operands are supported:  REG  memory  REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.  memory: [BX], [BX+SI+7], variable, etc...

MUL and IMUL instructions affect these flags only: CF, OF When result is over operand size these flags are set to 1, when result fits in operand size these flags are set to 0. For DIV and IDIV flags are undefined.

 MUL - Unsigned multiply:

when operand is a byte: AX = AL * operand. when operand is a word:(DX AX) = AX * operand.

 IMUL - Signed multiply:

when operand is a byte: AX = AL * operand. when operand is a word: (DX AX) = AX * operand.

 DIV - Unsigned divide:

when operand is a byte: AL = AX / operand AH = remainder (modulus). when operand is a word: AX = (DX AX) / operand DX = remainder (modulus).

 IDIV - Signed divide:

when operand is a byte: AL = AX / operand AH = remainder (modulus). when operand is a word: AX = (DX AX) / operand DX = remainder (modulus).

54 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

3. Third group: INC, DEC, NOT, NEG

These types of operands are supported:

 REG  Memory  REG: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP.  memory: [BX], [BX+SI+7], variable, etc... INC, DEC instructions affect these flags only: ZF, SF, OF, PF, AF.

NOT instruction does not affect any flags!

NEG instruction affects these flags only: CF, ZF, SF, OF, PF, AF.

 NOT - Reverse each bit of operand.

 NEG - Make operand negative (two's complement). Actually it reverses each bit of operand and then adds 1 to it. For example 5 will become -5, and -2 will become 2.

Review Questions: 1. Develop a short sequence that add AX, BX, CX, DX and SP, and Save the sum in DI 2. Write a program that adds a column of input values and produces the sum as output. The first input value will contain the number of values that follow as input to be added. 3. Write a program that solve the following equation: var4 = (var1 x -7) + (var2 % var3)

55 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (9): Program flow control Objectives  Identify unconditional jump Instructions.  Identify conditional jump Instructions.  Identify loop Instructions.

Controlling the program flow is a very important thing; this is where your program can make decisions according to certain conditions. Unconditional jumps The basic instruction that transfers control to another point in the program is JMP. The basic syntax of JMP instruction:

JMP label

To declare a label in your program, just type its name and add ":" to the end, label can be any character combination but it cannot start with a number, for example here are 3 legal label definitions:

label1: label2: a:

Label can be declared on a separate line or before any other instruction, for example:

x1: MOV AX, 1

x2: MOV AX, 2

Here’s an example of JMP instruction:

org 100h

mov ax, 5 ; set ax to 5. mov bx, 2 ; set bx to 2. jmp calc ; go to 'calc'. back: jmp stop ; go to 'stop'. calc: add ax, bx ; add bx to ax. jmp back ; go 'back'. stop: ret ; return to operating system.

Of course there is an easier way to calculate the sum of two numbers, but it's still a good example of JMP instruction. As you can see from this example JMP is able to transfer control both forward and backward. It can jump anywhere in current code segment (65,535 bytes).

56 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Short Conditional Jumps Unlike JMP instruction that does an unconditional jump, there are instructions that do a conditional jumps (jump only when some conditions are in act). These instructions are divided in three groups, first group just test single flag, second compares numbers as signed, and third compares numbers as unsigned. 1. Jump instructions that test single flag

Instruction Description Condition Opposite Instruction

JZ , JE Jump if Zero (Equal). ZF = 1 JNZ, JNE

JC , JB, JNAE Jump if Carry (Below, Not Above Equal). CF = 1 JNC, JNB, JAE

JS Jump if Sign. SF = 1 JNS

JO Jump if Overflow. OF = 1 JNO

JPE, JP Jump if Parity Even. PF = 1 JPO

JNZ , JNE Jump if Not Zero (Not Equal). ZF = 0 JZ, JE

JNC , JNB, JAE Jump if Not Carry (Not Below, Above Equal). CF = 0 JC, JB, JNAE

JNS Jump if Not Sign. SF = 0 JS

JNO Jump if Not Overflow. OF = 0 JO

JPO, JNP Jump if Parity Odd (No Parity). PF = 0 JPE, JP

As you may already notice there are some instructions that do that same thing, that's correct, they even are assembled into the same machine code, so it's good to remember that when you compile JE instruction - you will get it disassembled as: JZ, JC is assembled the same as JB etc... Different names are used to make programs easier to understand, to code and most importantly to remember. Very offset disassembler has no clue what the original instruction was look like that's why it uses the most common name. If you emulate this code you will see that all instructions are assembled into JNB, the operational code (opcode) for this instruction is 73h this instruction has fixed length of two bytes, the second byte is number of bytes to add to the IP register if the condition is true. Because the instruction has only 1 byte to keep the offset it is limited to pass control to -128 bytes back or 127 bytes forward, this value is always signed. jnc a jnb a jae a

mov ax, 4 a: mov ax, 5 ret

57 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

2. Jump instructions for signed numbers Instruction Description Condition Opposite Instruction

Jump if Equal (=). JE , JZ ZF = 1 JNE, JNZ Jump if Zero.

Jump if Not Equal (<>) JNE , JNZ ZF = 0 JE, JZ Jump if Not Zero.

ZF = 0 Jump if Greater (>) JG , JNLE and JNG, JLE Jump if Not Less or Equal (not <=). SF = OF

Jump if Less (<). JL , JNGE SF <> OF JNL, JGE Jump if Not Greater or Equal (not >=).

Jump if Greater or Equal (>=) JGE , JNL SF = OF JNGE, JL Jump if Not Less (not <).

ZF = 1 Jump if Less or Equal (<=) JLE , JNG or JNLE, JG Jump if Not Greater (not >). SF <> OF <> - sign means not equal.

3. Jump instructions for unsigned numbers Instruction Description Condition Opposite Instruction

Jump if Equal (=) JE , JZ ZF = 1 JNE, JNZ Jump if Zero.

Jump if Not Equal (<>) JNE , JNZ ZF = 0 JE, JZ Jump if Not Zero.

CF = 0 Jump if Above (>) JA , JNBE and JNA, JBE Jump if Not Below or Equal (not <=). ZF = 0

Jump if Below (<) JB , JNAE, JC Jump if Not Above or Equal (not >=). CF = 1 JNB, JAE, JNC Jump if Carry.

Jump if Above or Equal (>=). JAE , JNB, JNC Jump if Not Below (not <) CF = 0 JNAE, JB Jump if Not Carry.

CF = 1 Jump if Below or Equal (<=). JBE , JNA or JNBE, JA Jump if Not Above (not >). ZF = 1 Generally, when it is required to compare numeric values CMP instruction is used (it does the same as SUB (subtract) instruction, but does not keep the result, just affects the flags).

58 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

The logic is very simple, for example: It’s required to compare 5 and 2, 5 - 2 = 3 The result is not zero (Zero Flag is set to 0).

Another example: It’s required to compare 7 and 7, 7 - 7 = 0 The result is zero! (Zero Flag is set to 1 and JZ or JE will do the jump).

Here’s an example of CMP instruction and conditional jump: include "emu8086.inc"

org 100h

mov al, 25 ; set al to 25. mov bl, 10 ; set bl to 10.

cmp al, bl ; compare al - bl.

je equal ; jump if al = bl (zf = 1).

putc 'n' ; if it gets here, then al <> bl, jmp stop ; so print 'n', and jump to stop. equal: ; if gets here, putc 'y' ; then al = bl, so print 'y'.

stop:

ret ; gets here no matter what

Try the above example with different numbers for AL and BL, open flags by clicking on flags button, use single step and see what happens. You can use F5 hotkey to recompile and reload the program into the emulator.

Loops

Instruction Operation and jump condition Opposite instruction

LOOP Decrease CX, jump to label if CX not zero. DEC CX and JCXZ

LOOPE Decrease CX, jump to label if CX not zero and equal (zf = 1). LOOPNE

LOOPNE Decrease CX, jump to label if CX not zero and not equal (zf = 0). LOOPE

LOOPNZ Decrease CX, jump to label if CX not zero and zf = 0. LOOPZ

LOOPZ Decrease CX, jump to label if CX not zero and zf = 1. LOOPNZ

JCXZ Jump to label if CX is zero. OR CX, CX and JNZ

59 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Loops are basically the same jumps, it is possible to code loops without using the loop instruction, by just using conditional jumps and compare, and this is just what loop does. All loop instructions use CX register to count steps, as you know CX register has 16 bits and the maximum value it can hold is 65535 or FFFF, however with some agility it is possible to put one loop into another, and another into another two, and three and etc... and receive a nice value of 65535 * 65535 * 65535 ....till infinity.... or the end of ram or stack memory. It is possible store original value of CX register using push cx instruction and return it to original when the internal loop ends with pop cx, for example:

org 100h

mov bx, 0 ; total step counter.

mov cx, 5 k1: add bx, 1 mov al, '1' mov ah, 0eh int 10h push cx mov cx, 5 k2: add bx, 1 mov al, '2' mov ah, 0eh int 10h push cx mov cx, 5 k3: add bx, 1 mov al, '3' mov ah, 0eh int 10h loop k3 ; internal in internal loop. pop cx loop k2 ; internal loop. pop cx loop k1 ; external loop. ; wait any key... mov ah, 1 int 21h

ret

BX counts total number of steps, by default emulator shows values in hexadecimal, you can double click the register to see the value in all available bases.

All conditional jumps have one big limitation, unlike JMP instruction they can only jump 127 bytes forward and 128 bytes backward (note that most instructions are assembled into 3 or more bytes).

60 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Review Questions 1. Write a program that prints out the even numbers from 1 to 99. No input is required. 2. Write a program that accepts three values as input and outputs them in order of size, largest to smallest.

61 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Lab (10): Procedures, Interrupts and Stack Objectives  Learn how to execute Assembly instructions and observe the results Procedures

Procedure is a part of code that can be called from your program in order to make some specific task. Procedures make program more structural and easier to understand. Generally procedure returns to the same point from where it was called.

The syntax for procedure declaration: name PROC ; here goes the code ; of the procedure ... RET name ENDP

name - is the procedure name, the same name should be in the top and the bottom, this is used to check correct closing of procedures. Probably, you already know that RET instruction is used to return to operating system. The same instruction is used to return from procedure (actually operating system sees your program as a special procedure). PROC and ENDP are compiler directives, so they are not assembled into any real machine code. Compiler just remembers the address of procedure. CALL instruction is used to call a procedure. Here is an example: ORG 100h

CALL m1 MOV AX, 2 RET ; return to operating system.

m1 PROC MOV BX, 5 RET ; return to caller. m1 ENDP

END

The above example calls procedure m1, does MOV BX, 5, and returns to the next instruction after CALL: MOV AX, 2.

There are several ways to pass parameters to procedure, the easiest way to pass parameters is by using registers, here is another example of a procedure that receives two parameters in AL and BL registers, multiplies these parameters and returns the result in AX register:

62 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

ORG 100h

MOV AL, 1 MOV BL, 2

CALL m2 CALL m2 CALL m2 CALL m2

RET ; return to operating system.

m2 PROC MUL BL ; AX = AL * BL. RET ; return to caller. m2 ENDP

END

In the above example value of AL register is update every time the procedure is called, BL register stays unchanged, so this algorithm calculates 2 in power of 4, so final result in AX register is 16 (or 10h). Here goes another example, That uses a procedure to print a Hello World! message: ORG 100h

LEA SI, msg ; load address of msg to SI. CALL print_me RET ; return to operating system.

; this procedure prints a string, the string should be null ; terminated (have zero in the end), ; the string address should be in SI register: print_me PROC

next_char: CMP b.[SI], 0 ; check for zero to stop JE stop ; MOV AL, [SI] ; next get ASCII char. MOV AH, 0Eh ; teletype function number. INT 10h ; using interrupt to print a char in AL. ADD SI, 1 ; advance index of string array. JMP next_char ; go back, and type another char. stop: RET ; return to caller. print_me ENDP

msg DB 'Hello World!', 0 ; null terminated string. END

"b." - prefix before [SI] means that we need to compare bytes, not words. When you need to compare words add "w." prefix instead. When one of the compared operands is a register it's not required because compiler knows the size of each register.

63 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

Interrupts Interrupts can be seen as a number of functions. These functions make the programming much easier, instead of writing a code to print a character you can simply call the interrupt and it will do everything for you. There are also interrupt functions that work with disk drive and other hardware. We call such functions software interrupts. Interrupts are also triggered by different hardware, these are called hardware interrupts. Currently we are interested in software interrupts only. To make software interrupt there is an INT instruction, it has very simple syntax: INT value Where value can be a number between 0 to 255 (or 0 to 0FFh). Generally we will use hexadecimal numbers. You may think that there are only 256 functions, but that is not correct. Each interrupt may have sub-functions. To specify a sub-function AH register should be set before calling interrupt. Each interrupt may have up to 256 sub-functions (so we get 256 * 256 = 65536 functions). In general AH register is used, but sometimes other registers maybe in use. Generally other registers are used to pass parameters and data to sub-function. The following example uses INT 10h sub-function 0Eh to type a "Hello!" message. This function displays a character on the screen, advancing the cursor and scrolling the screen as necessary.

Example – Custom interrupt ; interrupt vector (memory from 00000h to 00400h) ; keeps addresses of all interrupts (from 00h to 0ffh). ; you can add new interrupt or modify existing interrupts. ; address of interrupt M is stored in vector at offset M * 4, ; for example: interrupt 10h is stored at offset 10h * 4. ; first goes the offset, then segment (total of 2 bytes). name "custint"

org 100h start:

; set video mode to 3 - 16 color 80x25 mov ah, 0 mov al, 3 int 10h ; set es to "0000": mov ax, 0 mov es, ax

mov bx, 90h ; calculate vector address for interrupt 90h: shl bx, 2 ; multiply 90h by 4, store result in bx: ; copy offset into interrupt vector: mov si, offset [test1] mov es:[bx], si add bx, 2

mov ax, cs ; copy segment into interrupt vector: mov es:[bx], ax

64 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

int 90h ; test newly created interrupt. ; wait for any key press: mov ah, 0 int 16h int 20h ; halt execution.

; interrupt 90h starts here: test1: pusha ; store all registers. ; make sure data segment is code segment: push cs pop ds

; set segment register to video memory: mov ax, 0b800h mov es, ax ; print message, each character is written as ; a word, high byte is color and low byte is ; ascii code: lea si, msg ; load offset of msg to si. mov di, 0 ; point to start of the screen. print: cmp [si], 0 ; if "0" then stop. je stop mov bl, [si] ; read ascii code from msg. mov bh, 0f1h ; set colors: white background, blue text. mov es:[di], bx ; write to video memory. add di, 2 ; go to next position on screen. inc si ; next char. jmp print stop: popa ; re-store all registers. iret ; return from interrupt. msg db "test of custom interrupt!", 0

65 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

The Stack Stack is an area of memory for keeping temporary data. Stack is used by CALL instruction to keep return address for procedure, RET instruction gets this value from the stack and returns to that offset. Quite the same thing happens when INT instruction calls an interrupt, it stores in stack flag register, code segment and offset. IRET instruction is used to return from interrupt call.

We can also use the stack to keep any other data; there are two instructions that work with the stack: PUSH - stores 16 bit value in the stack. POP - gets 16 bit value from the stack.

Syntax for PUSH instruction:

PUSH REG PUSH SREG PUSH memory PUSH immediate

REG: AX, BX, CX, DX, DI, SI, BP, SP SREG: DS, ES, SS, CS. memory: [BX], [BX+SI+7], 16 bit variable, etc... immediate: 5, -24, 3Fh, 10001101b, etc...

Syntax for POP instruction:

POP REG POP SREG POP memory

REG: AX, BX, CX, DX, DI, SI, BP, SP. SREG: DS, ES, SS, (except CS). memory: [BX], [BX+SI+7], 16 bit variable, etc...

Notes: PUSH and POP work with 16 bit values only! The stack uses LIFO (Last In First Out) algorithm, this means that if we push these values one by one into the stack: 1, 2, 3, 4, 5 The first value that we will get on pop will be 5, then 4, 3, 2, and only then 1. It is very important to do equal number of PUSHs and POPs, otherwise the stack maybe corrupted and it will be impossible to return to operating system. As you already know we use RET instruction to return to operating system, so when program starts there is a return address in stack (generally it's 0000h).

66 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

PUSH and POP instruction are especially useful because we don't have too much registers to operate with, so here is a trick:  Store original value of the register in stack (using PUSH).  Use the register for any purpose.  Restore the original value of the register from stack (using POP).

Here is an example:

ORG 100h

MOV AX, 1234h PUSH AX ; store value of AX in stack.

MOV AX, 5678h ; modify the AX value.

POP AX ; restore the original value of AX.

RET

END

Another use of the stack is for exchanging the values, here is an example:

ORG 100h

MOV AX, 1212h ; store 1212h in AX. MOV BX, 3434h ; store 3434h in BX

PUSH AX ; store value of AX in stack. PUSH BX ; store value of BX in stack.

POP AX ; set AX to original value of BX. POP BX ; set BX to original value of AX.

RET

END

The exchange happens because stack uses LIFO (Last In First Out) algorithm, so when we push 1212h and then 3434h, on pop we will first get 3434h and only after it 1212h.

The stack memory area is set by SS (Stack Segment) register, and SP (Stack Pointer) register. Generally operating system sets values of these registers on program start.

"PUSH source" instruction does the following:  Subtract 2 from SP register.  Write the value of source to the address SS:SP.

"POP destination" instruction does the following:  Write the value at the address SS:SP to destination.

67 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018

 Add 2 to SP register.

The current address pointed by SS:SP is called the top of the stack. Review Question 1. Write a procedure “WriteInt”, that receives a 16-bit signed integer in AX, converts it to string and print it on screen using DOS functions. 2. Write a procedure “ReadInt”, that receives a string from the keyboard and convert it to signed integer returned in AX register. 3. Write a program to evaluate the following arithmetic statement using a stack organized computer with zero-address instructions (so only pop and push can access memory).

68 Department of Computer Science CSC-210 Computer Organization and Assembly Language Laboratory Manual, 3rd Edition 2017/2018 Final Project Write an assembly language program to simulate the traffic light system implemented in Tabuk at 4-ways cross road. Your system must allow manual override, and online change of delays.

Time allowed: 2 weeks

Group: Maximum two students.

All Works must be conducted in the lab.

Software: EMU8086 emulator

69 Appendix (A) 06/10/17

CHAPTER 2: Number Systems

The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander John Wiley and Sons ©2003

Linda Senne, Bentley College Wilson Wong, Bentley College

Why Binary?

§ Early computer design was decimal § Mark I and ENIAC § John von Neumann proposed binary data processing (1945) § Simplified computer design § Used for both instructions and data § Natural relationship between on/off switches and On Off calculation using Boolean logic True False Yes No 1 0 Chapter 2 Number Systems 2-2

1 06/10/17

Counting and Arithmetic § Decimal or base 10 number system § Origin: counting on the fingers § “Digit” from the Latin word digitus meaning “finger” § Base: the number of different digits including zero in the number system § Example: Base 10 has 10 digits, 0 through 9 § Binary or base 2 § Bit (binary digit): 2 digits, 0 and 1 § Octal or base 8: 8 digits, 0 through 7 § Hexadecimal or base 16: 16 digits, 0 through F

Chapter§ 2 NumberExamples: Systems 1010 = A16; 1110 = B16 2-3

Keeping Track of the Bits

§ Bits commonly stored and manipulated in groups § 8 bits = 1 byte § 4 bytes = 1 word (in many systems) § Number of bits used in calculations § Affects accuracy of results § Limits size of numbers manipulated by the computer

Chapter 2 Number Systems 2-4

2 06/10/17

Numbers: Physical Representation

§ Different numerals, same number of oranges § Cave dweller: IIIII § Roman: V § Arabic: 5 § Different bases, same number of oranges

§ 510

§ 1012

§ 123

Chapter 2 Number Systems 2-5

Number System

§ Roman: position independent § Modern: based on positional notation (place value) § Decimal system: system of positional notation based on powers of 10. § Binary system: system of positional notation based powers of 2 § Octal system: system of positional notation based on powers of 8 § Hexadecimal system: system of positional notation based powers of 16

Chapter 2 Number Systems 2-6

3 06/10/17

Positional Notation: Base 10

43 = 4 x 101 + 3 x 100

10’s 1’s place place

Place 101 100

Value 10 1

Evaluate 4 x 10 3 x1

Sum 40 3

Chapter 2 Number Systems 2-7

Positional Notation: Base 10

527 = 5 x 102 + 2 x 101 + 7 x 100 ’ 100’s place 10’s 1 s place place

Place 102 101 100

Value 100 10 1

Evaluate 5 x 100 2 x 10 7 x1

Sum 500 20 7

Chapter 2 Number Systems 2-8

4 06/10/17

Positional Notation: Octal

6248 = 40410

64’s place 8’s place 1’s place

Place 82 81 80

Value 64 8 1

Evaluate 6 x 64 2 x 8 4 x 1 Sum for 384 16 4 Base 10

Chapter 2 Number Systems 2-9

Positional Notation: Hexadecimal

6,70416 = 26,37210 4,096’s place 256’s place 16’s 1’s place place

Place 163 162 161 160

Value 4,096 256 16 1

Evaluate 6 x 7 x 256 0 x 16 4 x 1 4,096 Sum for 24,576 1,792 0 4 Base 10

Chapter 2 Number Systems 2-10

5 06/10/17

Positional Notation: Binary

1101 01102 = 21410

Place 27 26 25 24 23 22 21 20

Value 128 64 32 16 8 4 2 1

Evaluate 1 x 128 1 x 64 0 x 32 1 x16 0 x 8 1 x 4 1 x 2 0 x 1

Sum for 128 64 0 16 0 4 2 0 Base 10

Chapter 2 Number Systems 2-11

Estimating Magnitude: Binary

1101 01102 = 21410

1101 01102 > 19210 (128 + 64 + additional bits to the right)

Place 27 26 25 24 23 22 21 20

Value 128 64 32 16 8 4 2 1

Evaluate 1 x 128 1 x 64 0 x 32 1 x16 0 x 8 1 x 4 1 x 2 0 x 1

Sum for 128 64 0 16 0 4 2 0 Base 10

Chapter 2 Number Systems 2-12

6 06/10/17

Range of Possible Numbers

§ R = BK where § R = range § B = base § K = number of digits § Example #1: Base 10, 2 digits § R = 102 = 100 different numbers (0…99) § Example #2: Base 2, 16 digits § R = 216 = 65,536 or 64K § 16-bit PC can store 65,536 different number values

Chapter 2 Number Systems 2-13

Decimal Range for Bit Widths

Bits Digits Range 1 0+ 2 (0 and 1) 4 1+ 16 (0 to 15) 8 2+ 256 10 3 1,024 (1K) 16 4+ 65,536 (64K) 20 6 1,048,576 (1M) 32 9+ 4,294,967,296 (4G) 64 19+ Approx. 1.6 x 1019 128 38+ Approx. 2.6 x 1038 Chapter 2 Number Systems 2-14

7 06/10/17

Base or Radix

§ Base: § The number of different symbols required to represent any given number § The larger the base, the more numerals are required

§ Base 10: 0,1, 2,3,4,5,6,7,8,9

§ Base 2: 0,1

§ Base 8: 0,1,2, 3,4,5,6,7

§ Base 16: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Chapter 2 Number Systems 2-15

Number of Symbols vs. Number of Digits § For a given number, the larger the base § the more symbols required § but the fewer digits needed § Example #1:

§ 6516 10110 1458 110 01012 § Example #2:

§ 11C16 28410 4348 1 0001 11002

Chapter 2 Number Systems 2-16

8 06/10/17

Counting in Base 2

Binary Equivalent Decimal Number 8’s (23) 4’s (22) 2’s (21) 1’s (20) Number 0 0 x 20 0 1 1 x 20 1 10 1 x 21 0 x 20 2 11 1 x 21 1 x 20 3 100 1 x 22 4 101 1 x 22 1 x 20 5 110 1 x 22 1 x 21 6 111 1 x 22 1 x 21 1 x 20 7 1000 1 x 23 8 1001 1 x 23 1 x 20 9 1010 1 x 23 1 x 21 10 Chapter 2 Number Systems 2-17

Base 10 Addition Table

310 + 610 = 910 + 0 1 2 3 4 5 6 7 8 9

0 0 1 2 3 4 5 6 7 8 9

1 1 2 3 4 5 6 7 8 9 10

2 2 3 4 5 6 7 8 9 10 11

3 3 4 5 6 7 8 9 10 11 12

4 4 5 6 7 8 9 10 11 12 13 etc

Chapter 2 Number Systems 2-18

9 06/10/17

Base 8 Addition Table

38 + 68 = 118 + 0 1 2 3 4 5 6 7 0 0 1 2 3 4 5 6 7

1 1 2 3 4 5 6 7 10 (no 8 or 9, 2 2 3 4 5 6 7 10 11 of course)

3 3 4 5 6 7 10 11 12

4 4 5 6 7 10 11 12 13

5 5 6 7 10 11 12 13 14

6 6 7 10 11 12 13 14 15

7 7 10 11 12 13 14 15 16

Chapter 2 Number Systems 2-19

Base 10 Multiplication Table

310 x 610 = 1810 x 0 1 2 3 4 5 6 7 8 9 0 0

1 1 2 3 4 5 6 7 8 9

2 2 4 6 8 10 12 14 16 18

3 3 6 9 12 15 18 21 24 27

4 0 4 8 12 16 20 24 28 32 36

5 5 10 15 20 25 30 35 40 45

6 6 12 18 24 30 36 42 48 54

7 7 14 21 28 35 42 49 56 63

etc. Chapter 2 Number Systems 2-20

10 06/10/17

Base 8 Multiplication Table

38 x 68 = 228 x 0 1 2 3 4 5 6 7 0 0

1 1 2 3 4 5 6 7

2 2 4 6 10 12 14 16

3 0 3 6 11 14 17 22 25

4 4 10 14 20 24 30 34

5 5 12 17 24 31 36 43

6 6 14 22 30 36 44 52

7 7 16 25 34 43 52 61

Chapter 2 Number Systems 2-21

Addition

Base Problem Largest Single Digit

6 Decimal +3 9

6 Octal +1 7 6 Hexadecimal +9 F 1 Binary +0 1

Chapter 2 Number Systems 2-22

11 06/10/17

Addition

Base Problem Carry Answer 6 Decimal +4 Carry the 10 10

6 Octal +2 Carry the 8 10 6 Hexadecimal +A Carry the 16 10 1 Binary +1 Carry the 2 10

Chapter 2 Number Systems 2-23

Binary Arithmetic

1 1 1 1 1 1 1 0 1 1 0 1 + 1 0 1 1 0 1 0 0 0 0 0 1 1

Chapter 2 Number Systems 2-24

12 06/10/17

Binary Arithmetic

§ Addition § Boolean using + 0 1 XOR and AND 0 0 1 § Multiplication 1 1 10 § AND § Shift x 0 1 § Division 0 0 0 1 0 1

Chapter 2 Number Systems 2-25

Binary Arithmetic: Boolean Logic

§ Boolean logic without performing arithmetic § EXCLUSIVE-OR

p Output is “1” only if either input, but not both inputs, is a “1” § AND (carry bit)

p Output is “1” if and only both inputs are a “1”

1 1 1 1 1 1 1 0 1 1 0 1 + 1 0 1 1 0 1 0 0 0 0 0 1 1

Chapter 2 Number Systems 2-26

13 06/10/17

Binary Multiplication

§ Boolean logic without performing arithmetic § AND (carry bit)

p Output is “1” if and only both inputs are a “1” § Shift

p Shifting a number in any base left one digit multiplies its value by the base

p Shifting a number in any base right one digit divides its value by the base

p Examples:

p 1010 shift left = 10010 p 1010 shift right = 110

p 102 shift left = 1002 p 102 shift right = 12 Chapter 2 Number Systems 2-27

Binary Multiplication

1 1 0 1 1 0 1

1 1 0 1 1’s place

0 2’s place

1 1 0 1 4’s place (bits shifted to line up with 4’s place of multiplier)

1 0 0 0 0 0 1 Result (AND)

Chapter 2 Number Systems 2-28

14 06/10/17

Binary Multiplication

1 1 0 1 1 0 1

x 1 0 0 1 1 0 2’s place (bits shifted to line 1 1 0 1 1 0 1 up with 2’s place of multiplier)

1 1 0 1 1 0 1 4’s place

1 1 0 1 1 0 1 32’s place Result (AND) 1 0 0 0 0 0 0 1 0 1 1 1 0 Note the 0 at the end, since the 1’s place is not brought down.

Note: multiple carries are possible.

Chapter 2 Number Systems 2-29

Converting from Base 10

§ Powers Table

Power 8 7 6 5 4 3 2 1 0 Base

2 256 128 64 32 16 8 4 2 1

8 32,768 4,096 512 64 8 1

16 65,536 4,096 256 16 1

Chapter 2 Number Systems 2-30

15 06/10/17

From Base 10 to Base 2

4210 = 1010102

Power Base 6 5 4 3 2 1 0

2 64 32 16 8 4 2 1

1 0 1 0 1 0

Integer 42/32 10/16 10/8 2/4 2/2 0/1 = 1 = 0 = 1 = 0 = 1 = 0

Remainder 10 10 2 2 0 0

Chapter 2 Number Systems 2-31

From Base 10 to Base 2

Base 10 42 Remainder

Quotient 2 ) 42 ( 0 Least significant bit 2 ) 21 ( 1 2 ) 10 ( 0 2 ) 5 ( 1 2 ) 2 ( 0 2 ) 1 Most significant bit Base 2 101010

Chapter 2 Number Systems 2-32

16 06/10/17

From Base 10 to Base 16

5,73510 = 166716

Power Base 4 3 2 1 0

16 65,536 4,096 256 16 1

1 6 6 7 Integer 5,735 /4,096 1,639 / 256 103 /16 7 = 1 = 6 = 6 Remainder 5,735 - 4,096 1,639 –1,536 103 – 96 = 1,639 = 103 = 7

Chapter 2 Number Systems 2-33

From Base 10 to Base 16

Base 10 5,735 Remainder

Quotient 16 ) 5,735 ( 7 Least significant bit 16 ) 358 ( 6 16 ) 22 ( 6 16 ) 1 ( 1 Most significant bit 16 ) 0 Base 16 1667

Chapter 2 Number Systems 2-34

17 06/10/17

From Base 10 to Base 16

Base 10 8,039 Remainder

Quotient 16 ) 8,039 ( 7 Least significant bit 16 ) 502 ( 6 16 ) 31 ( 15 16 ) 1 ( 1 Most significant bit 16 ) 0 Base 16 1F67

Chapter 2 Number Systems 2-35

From Base 8 to Base 10

72638 = 3,76310

Power 83 82 81 80

512 64 8 1

x 7 x 2 x 6 x 3 Sum for Base 10 3,584 128 48 3

Chapter 2 Number Systems 2-36

18 06/10/17

From Base 8 to Base 10

72638 = 3,76310 7 x 8 56 + 2 = 58 x 8 464 + 6 = 470 x 8 3760 + 3 = 3,763

Chapter 2 Number Systems 2-37

From Base 16 to Base 2

§ The nibble approach § Hex easier to read and write than binary

Base 16 1 F 6 7

Base 2 0001 1111 0110 0111

§ Why hexadecimal?

p Modern computer operating systems and networks present variety of troubleshooting data in hex format

Chapter 2 Number Systems 2-38

19 06/10/17

Fractions

§ Number point or radix point § Decimal point in base 10 § Binary point in base 2 § No exact relationship between fractional numbers in different number bases § Exact conversion may be impossible

Chapter 2 Number Systems 2-39

Decimal Fractions

§ Move the number point one place to the right § Effect: multiplies the number by the base number

§ Example: 139.010 139010 § Move the number point one place to the left § Effect: divides the number by the base number

§ Example: 139.010 13.910

Chapter 2 Number Systems 2-40

20 06/10/17

Fractions: Base 10 and Base 2

.258910 Place 10-1 10-2 10-3 10-4 Value 1/10 1/100 1/1000 1/10000 Evaluate 2 x 1/10 5 x 1/100 8 x 1/1000 9 x1/1000 Sum .2 .05 .008 .0009

.1010112 = 0.67187510

Place 2-1 2-2 2-3 2-4 2-5 2-6

Value 1/2 1/4 1/8 1/16 1/32 1/64

Evaluate 1 x 1/2 0 x 1/4 1x 1/8 0 x 1/16 1 x 1/32 1 x 1/64

Sum .5 0.125 0.03125 0.015625

Chapter 2 Number Systems 2-41

Fractions: Base 10 and Base 2

§ No general relationship between fractions of types 1/10k and 1/2k § Therefore a number representable in base 10 may not be representable in base 2 § But: the converse is true: all fractions of the form 1/2k can be represented in base 10 § Fractional conversions from one base to another are stopped § If there is a rational solution or § When the desired accuracy is attained

Chapter 2 Number Systems 2-42

21 06/10/17

Mixed Number Conversion

§ Integer and fraction parts must be converted separately § Radix point: fixed reference for the conversion § Digit to the left is a unit digit in every base § B0 is always 1 regardless of the base

Chapter 2 Number Systems 2-43

Copyright 2003 John Wiley & Sons

All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department, John Wiley & Songs, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information contained herein.”

Chapter 2 Number Systems 2-44

22 Appendix (B) Intel Assembler © 1996-2003 by Roger Jegerlehner, Switzerland 80186 and higher CodeTable 1/2 V 2.3 English. Also available in Spanish TRANSFER Flags Name Comment Code Operation O D I T S Z A P C MOV Move (copy) MOV Dest,Source Dest:=Source XCHG Exchange XCHG Op1,Op2 Op1:=Op2 , Op2:=Op1 STC Set Carry STC CF:=1 1 CLC Clear Carry CLC CF:=0 0 CMC Complement Carry CMC CF:= ØCF ± STD Set Direction STD DF:=1 (string op's downwards) 1 CLD Clear Direction CLD DF:=0 (string op's upwards) 0 STI Set Interrupt STI IF:=1 1 CLI Clear Interrupt CLI IF:=0 0 PUSH Push onto stack PUSH Source DEC SP, [SP]:=Source PUSHF Push flags PUSHF O, D, I, T, S, Z, A, P, C 286+: also NT, IOPL PUSHA Push all general registers PUSHA AX, CX, DX, BX, SP, BP, SI, DI POP Pop from stack POP Dest Dest:=[SP], INC SP POPF Pop flags POPF O, D, I, T, S, Z, A, P, C 286+: also NT, IOPL ± ± ± ± ± ± ± ± ± POPA Pop all general registers POPA DI, SI, BP, SP, BX, DX, CX, AX CBW Convert byte to word CBW AX:=AL (signed) CWD Convert word to double CWD DX:AX:=AX (signed) ± ± ± ± ± ± CWDE Conv word extended double CWDE 386 EAX:=AX (signed) IN i Input IN Dest, Port AL/AX/EAX := byte/word/double of specified port OUT i Output OUT Port, Source Byte/word/double of specified port := AL/AX/EAX i for more information see instruction specifications Flags: ±=affected by this instruction ?=undefined after this instruction ARITHMETIC Flags Name Comment Code Operation O D I T S Z A P C ADD Add ADD Dest,Source Dest:=Dest+Source ± ± ± ± ± ± ADC Add with Carry ADC Dest,Source Dest:=Dest+Source+CF ± ± ± ± ± ± SUB Subtract SUB Dest,Source Dest:=Dest-Source ± ± ± ± ± ± SBB Subtract with borrow SBB Dest,Source Dest:=Dest-(Source+CF) ± ± ± ± ± ± DIV Divide (unsigned) DIV Op Op=byte: AL:=AX / Op AH:=Rest ? ????? DIV Divide (unsigned) DIV Op Op=word: AX:=DX:AX / Op DX:=Rest ? ????? DIV 386 Divide (unsigned) DIV Op Op=doublew.: EAX:=EDX:EAX / Op EDX:=Rest ? ????? IDIV Signed Integer Divide IDIV Op Op=byte: AL:=AX / Op AH:=Rest ? ????? IDIV Signed Integer Divide IDIV Op Op=word: AX:=DX:AX / Op DX:=Rest ? ????? IDIV 386 Signed Integer Divide IDIV Op Op=doublew.: EAX:=EDX:EAX / Op EDX:=Rest ? ????? MUL Multiply (unsigned) MUL Op Op=byte: AX:=AL*Op if AH=0 ¨ ± ???? ± MUL Multiply (unsigned) MUL Op Op=word: DX:AX:=AX*Op if DX=0 ¨ ± ???? ± MUL 386 Multiply (unsigned) MUL Op Op=double: EDX:EAX:=EAX*Op if EDX=0 ¨ ± ???? ± IMUL i Signed Integer Multiply IMUL Op Op=byte: AX:=AL*Op if AL sufficient ¨ ± ???? ± IMUL Signed Integer Multiply IMUL Op Op=word: DX:AX:=AX*Op if AX sufficient ¨ ± ???? ± IMUL 386 Signed Integer Multiply IMUL Op Op=double: EDX:EAX:=EAX*Op if EAX sufficient ¨ ± ???? ± INC Increment INC Op Op:=Op+1 (Carry not affected !) ± ± ± ± ± DEC Decrement DEC Op Op:=Op-1 (Carry not affected !) ± ± ± ± ± CMP Compare CMP Op1,Op2 Op1-Op2 ± ± ± ± ± ± SAL Shift arithmetic left (º SHL) SAL Op,Quantity i ± ± ? ± ± SAR Shift arithmetic right SAR Op,Quantity i ± ± ? ± ± RCL Rotate left through Carry RCL Op,Quantity i ± RCR Rotate right through Carry RCR Op,Quantity i ± ROL Rotate left ROL Op,Quantity i ± ROR Rotate right ROR Op,Quantity i ± i for more information see instruction specifications ¨ then CF:=0, OF:=0 else CF:=1, OF:=1 LOGIC Flags Name Comment Code Operation O D I T S Z A P C NEG Negate (two-complement) NEG Op Op:=0-Op if Op=0 then CF:=0 else CF:=1 ± ± ± ± ± ± NOT Invert each bit NOT Op Op:=ØOp (invert each bit) AND Logical and AND Dest,Source Dest:=DestÙSource 0 ± ± ? ± 0 OR Logical or OR Dest,Source Dest:=DestÚSource 0 ± ± ? ± 0 XOR Logical exclusive or XOR Dest,Source Dest:=Dest (exor) Source 0 ± ± ? ± 0 SHL Shift logical left (º SAL) SHL Op,Quantity i ± ± ? ± ± SHR Shift logical right SHR Op,Quantity i ± ± ? ± ± Download latest version free of charge from www.jegerlehner.ch/intel This page may be freely distributed without cost provided it is not changed. All rights reserved Intel Assembler © 1996-2003 by Roger Jegerlehner, Switzerland 80186 and higher CodeTable 2/2 V 2.3 English. Also available in Spanish MISC Flags Name Comment Code Operation O D I T S Z A P C NOP No operation NOP No operation LEA Load effective address LEA Dest,Source Dest := address of Source INT Interrupt INT Nr interrupts current program, runs spec. int-program 0 0

JUMPS (flags remain unchanged) Name Comment Code Operation Name Comment Code Operation CALL Call subroutine CALL Proc RET Return from subroutine RET JMP Jump JMP Dest JE Jump if Equal JE Dest (º JZ) JNE Jump if not Equal JNE Dest (º JNZ) JZ Jump if Zero JZ Dest (º JE) JNZ Jump if not Zero JNZ Dest (º JNE) JCXZ Jump if CX Zero JCXZ Dest JECXZ Jump if ECX Zero JECXZ Dest 386 JP Jump if Parity (Parity Even) JP Dest (º JPE) JNP Jump if no Parity (Parity Odd) JNP Dest (º JPO) JPE Jump if Parity Even JPE Dest (º JP) JPO Jump if Parity Odd JPO Dest (º JNP)

JUMPS Unsigned (Cardinal) JUMPS Signed (Integer) JA Jump if Above JA Dest (º JNBE) JG Jump if Greater JG Dest (º JNLE) JAE Jump if Above or Equal JAE Dest (º JNB º JNC) JGE Jump if Greater or Equal JGE Dest (º JNL) JB Jump if Below JB Dest (º JNAE º JC) JL Jump if Less JL Dest (º JNGE) JBE Jump if Below or Equal JBE Dest (º JNA) JLE Jump if Less or Equal JLE Dest (º JNG) JNA Jump if not Above JNA Dest (º JBE) JNG Jump if not Greater JNG Dest (º JLE) JNAE Jump if not Above or Equal JNAE Dest (º JB º JC) JNGE Jump if not Greater or Equal JNGE Dest (º JL) JNB Jump if not Below JNB Dest (º JAE º JNC) JNL Jump if not Less JNL Dest (º JGE) JNBE Jump if not Below or Equal JNBE Dest (º JA) JNLE Jump if not Less or Equal JNLE Dest (º JG) JC Jump if Carry JC Dest JO Jump if Overflow JO Dest JNC Jump if no Carry JNC Dest JNO Jump if no Overflow JNO Dest JS Jump if Sign (= negative) JS Dest General Registers: JNS Jump if no Sign (= positive) JNS Dest

EAX 386 Example: AX .DOSSEG ; Demo program AH AL .MODEL SMALL Accumulator .STACK 1024 31 24 23 16 15 8 7 0 Two EQU 2 ; Const .DATA EDX 386 VarB DB ? ; define Byte, any value DX VarW DW 1010b ; define Word, binary DH DL VarW2 DW 257 ; define Word, decimal Data mul, div, IO VarD DD 0AFFFFh ; define Doubleword, hex 31 24 23 16 15 8 7 0 S DB "Hello !",0 ; define String .CODE ECX 386 main: MOV AX,DGROUP ; resolved by linker CX MOV DS,AX ; init datasegment reg CH CL MOV [VarB],42 ; init VarB Count loop, shift MOV [VarD],-7 ; set VarD 31 24 23 16 15 8 7 0 MOV BX,Offset[S] ; addr of "H" of "Hello !" MOV AX,[VarW] ; get value into accumulator EBX 386 ADD AX,[VarW2] ; add VarW2 to AX BX MOV [VarW2],AX ; store AX in VarW2 BH BL MOV AX,4C00h ; back to system BaseX data ptr INT 21h 31 24 23 16 15 8 7 0 END main

Flags: --- - O D I T S - A - P - C Status Flags (result of operations): C: Carry result of unsigned op. is too large or below zero. 1 = carry/borrow Control Flags (how instructions are carried out): O: Overflow result of signed op. is too large or small. 1 = overflow/underflow D: Direction 1 = string op's process down from high to low address S: Sign sign of result. Reasonable for Integer only. 1 = neg. / 0 = pos. I: Interrupt whether interrupts can occur. 1= enabled Z: Zero result of operation is zero. 1 = zero T: Trap single step for debugging A: Aux. carry similar to Carry but restricted to the low nibble only P: Parity 1 = result has even number of set bits Download latest version free of charge from www.jegerlehner.ch/intel This page may be freely distributed without cost provided it is not changed. All rights reserved