Docsity

Display Memory Formation-Microprocessor and Assembly Language Programming-Lecture Notes, Study notes of Microprocessor and Assembly Language Programming

Toggle sidebar
bg1
The computer listens, sees, and speaks in numbers. Even a character is a
number inside the computer. For example the keyboard is labeled with
characters however when we press ‘A’, a specific number is transferred from
the keyboard to the computer. Our program interprets that number as the
character ‘A’. When the same number comes on display, the Video Graphics
Adapter (VGA) in our computer shows the shape of ‘A’. Even the shape is
stored in binary numbers with a one bit representing a pixel on the screen
that is turned on and a zero bit representing a pixel that is not glowing. This
example is considering a white on black display and no colors. This is the
way a shape is drawn on the screen. The interpretation of ‘A’ is performed by
the VGA card, while the monitor or CRT (cathode ray tube) only glows the
pixels on and turns them off. The keyboard has a key labeled ‘A’ and
pressing it the screen shows ‘A’ but all that happened inside was in
numbers.
An ‘A’ on any computer and any operating system is an ‘A’ on every other
computer and operating system. This is because a standard numeric
representation of all commonly used characters has been developed. This is
called the ASCII code, where ASCII stands for American Standard Code for
Information Interchange. The name depicts that this is a code that allows the
interchange of information; ‘A’ written on one computer will remain an ‘A’ on
another. The ASCII table lists all defined characters and symbols and their
standardized numbers. All ASCII based computers use the same code. There
are few other standards like EBCDIC and gray codes, but ASCII has become
the most prevalent standard and is used for Internet communication as well.
It has become the de facto standard for global communication. The character
mode displays of our computer use the ASCII standard. Some newer
operating systems use a new standard Unicode but it is not relevant to us in
the current discussion.
Standard ASCII has 128 characters with numbers assigned from 0 to 127.
When IBM PC was introduced, they extended the standard ASCII and defined
128 more characters. Thus extending the total number of symbols from 128
to 256 numbered from 0 to 255 fitting in an 8-bit byte. The newer characters
were used for line drawing, window corners, and some non-English
characters. The need for these characters was never felt on teletype
terminals, but with the advent of IBM PC and its full screen display, these
semi-graphics characters were the need of the day. Keep in mind that at that
time there was no graphics mode available.
The extended ASCII code is just a de facto industry standard but it is not
defined by an organization like the standard ASCII. Printers, displays, and all
other peripherals related to the IBM PC understand the ASCII code. If the
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Display Memory Formation-Microprocessor and Assembly Language Programming-Lecture Notes and more Study notes Microprocessor and Assembly Language Programming in PDF only on Docsity!

The computer listens, sees, and speaks in numbers. Even a character is a

number inside the computer. For example the keyboard is labeled with

characters however when we press ‘A’, a specific number is transferred from

the keyboard to the computer. Our program interprets that number as the

character ‘A’. When the same number comes on display, the Video Graphics

Adapter (VGA) in our computer shows the shape of ‘A’. Even the shape is

stored in binary numbers with a one bit representing a pixel on the screen

that is turned on and a zero bit representing a pixel that is not glowing. This

example is considering a white on black display and no colors. This is the

way a shape is drawn on the screen. The interpretation of ‘A’ is performed by

the VGA card, while the monitor or CRT (cathode ray tube) only glows the

pixels on and turns them off. The keyboard has a key labeled ‘A’ and

pressing it the screen shows ‘A’ but all that happened inside was in

numbers.

An ‘A’ on any computer and any operating system is an ‘A’ on every other

computer and operating system. This is because a standard numeric

representation of all commonly used characters has been developed. This is

called the ASCII code, where ASCII stands for American Standard Code for

Information Interchange. The name depicts that this is a code that allows the

interchange of information; ‘A’ written on one computer will remain an ‘A’ on

another. The ASCII table lists all defined characters and symbols and their

standardized numbers. All ASCII based computers use the same code. There

are few other standards like EBCDIC and gray codes, but ASCII has become

the most prevalent standard and is used for Internet communication as well.

It has become the de facto standard for global communication. The character

mode displays of our computer use the ASCII standard. Some newer

operating systems use a new standard Unicode but it is not relevant to us in

the current discussion.

Standard ASCII has 128 characters with numbers assigned from 0 to 127.

When IBM PC was introduced, they extended the standard ASCII and defined

128 more characters. Thus extending the total number of symbols from 128

to 256 numbered from 0 to 255 fitting in an 8-bit byte. The newer characters

were used for line drawing, window corners, and some non-English

characters. The need for these characters was never felt on teletype

terminals, but with the advent of IBM PC and its full screen display, these

semi-graphics characters were the need of the day. Keep in mind that at that

time there was no graphics mode available.

The extended ASCII code is just a de facto industry standard but it is not

defined by an organization like the standard ASCII. Printers, displays, and all

other peripherals related to the IBM PC understand the ASCII code. If the

code for ‘A’ is sent to the printer, the printer will print the shape of ‘A’, if it is

sent to the display, the VGA card will form the shape of ‘A’ on the CRT. If it is

sent to another computer via the serial port, the other computer will

understand that this is an ‘A’.

The important thing to observe in the ASCII table is the contiguous

arrangement of the uppercase alphabets (41-5A), the lowercase alphabets

(61-7A), and the numbers (30-39). This helps in certain operations with

ASCII, for example converting the case of characters by adding or subtracting

0x20 from it. It also helps in converting a digit into its ASCII representation

by adding 0x30 to it.

DISPLAY MEMORY FORMATION

We will explore the working of the display with ASCII codes, since it is our

immediately accessible hardware. When 0x40 is sent to the VGA card, it will

turn pixels on and off in such a way that a visual representation of ‘A’

appears on the screen. It has no reality, just an interpretation. In later

chapters we will program the VGA controller to display a new shape when

the ASCII of ‘A’ is received by it.

The video device is seen by the computer as a memory area containing the

ASCII codes that are currently displayed on the screen and a set of I/O ports

controlling things like the resolution, the cursor height, and the cursor

position. The VGA memory is seen by the computer just like its own memory.

There is no difference; rather the computer doesn’t differentiate, as it is

accessible on the same bus as the system memory. Therefore if that

appropriate block of the screen is cleared, the screen will be cleared. If the

ASCII of ‘A’ is placed somewhere in that block, the shape of ‘A’ will appear on

the screen at a corresponding place.

This correspondence must be defined as the memory is a single

dimensional space while the screen is two dimensional having 80 rows and

25 columns. The memory is linearly mapped on this two dimensional space,

just like a two dimensional is mapped in linear memory. There is one word

per character in which a byte is needed for the ASCII code and the other byte

is used for the character’s attributes discussed later. Now the first 80 words

will correspond to the first row of the screen and the next 80 words will

correspond to the next row. By making the memory on the video controller

accessible to the processor via the system bus, the processor is now in

control of what is displayed on the screen.

The three important things that we discussed are.

 One screen location corresponds to a word in the video memory

 The video controller memory is accessible to the processor like its

own memory.

 ASCII code of a character placed at a cell in the VGA memory will

cause the corresponding ASCII shape to be displayed on the

corresponding screen location.

Display Memory Base Address

The memory at which the video controller’s memory is mapped must be a

standard, so that the program can be written in a video card independent

manner. Otherwise if different vendors map their video memory at different

places in the address space, as was the problem in the start, writing software

was a headache. BIOS vendors had a problem of dealing with various card

vendors. The IBM PC text mode color display is now fixed so that system

software can work uniformly. It was fixed at the physical memory location of

B8000. The first byte at this location contains the ASCII for the character

displayed at the top left of the video screen. Dropping the zero we can load

the rest in a segment register to access the video memory. If we do something

in this memory, the effect can be seen on the screen. For example we can

; clear the screen [org 0x0100] mov ax, 0xb800 ; load video base in ax mov es, ax ; point es to video base mov di, 0 ; point di to top left column

nextchar: mov word [es:di], 0x0720 ; clear next char on screen add di, 2 ; move to next screen location cmp di, 4000 ; has the whole screen cleared jne nextchar ; if no clear next position

mov ax, 0x4c00 ; terminate program int 0x

The code for space is 20 while 07 is the normal attribute of low

intensity white on black with no blinking. Even to clear the screen or

put a blank on a location there is a numeric code.

DI is incremented twice since each screen location corresponds to

two byte in video memory.

DI is compared with 80252=4000. The last word location that

corresponds to the screen is 3998.

Inside the debugger the operation of clearing the screen cannot be

observed since the debugger overwrites whatever is displayed on the screen.

Directly executing the COM file from the command prompt *^ , we can see that

the screen is cleared. The command prompt that reappeared is printed after

the termination of our application. This is the first application that can be

directly executed to see some output on the screen.

HELLO WORLD IN ASSEMBLY LANGUAGE

To declare a character in assembly language, we store its ASCII code in a

byte. The assembler provides us with another syntax that doesn’t forces us to

remember the ASCII code. The assembler also provides a syntax that

simplifies declaration of consecutive characters, usually called a string. The

three ways used below are identical in their meaning.

db 0x61, 0x6 2 , 0x db 'a', 'b', 'c' db 'abc'

When characters are stored in any high level or low level language the

actual thing stored in a byte is their ASCII code. The only thing the language

helps in is a simplified declaration.

Traditionally the first program in higher level languages is to print “hello

world” on the screen. However due to the highly granular nature of assembly

language, we are only now able to write it in assembly language. In writing

this program, we make a generic routine that can print any string on the

screen.

Example

; hello world in assembly [org 0x0100] jmp start

message: db 'hello world' ; string to be printed length: dw 11 ; length of the string

; subroutine to clear the screen clrscr: push es push ax push di

* Remember that if this example is run in a DOS window on some newer

operating systems, a full screen DOS application must be run before this

program so that screen access is enabled.

mov ax, 0xb mov es, ax ; point es to video base mov di, 0 ; point di to top left column

nextloc: mov word [es:di], 0x0720 ; clear next char on screen add di, 2 ; move to next screen location cmp di, 4000 ; has the whole screen cleared jne nextloc ; if no clear next position

pop di pop ax pop es ret

; subroutine to print a string at top left of screen ; takes address of string and its length as parameters printstr: push bp mov bp, sp push es push ax push cx push si push di

mov ax, 0xb mov es, ax ; point es to video base mov di, 0 ; point di to top left column mov si, [bp+6] ; point si to string mov cx, [bp+4] ; load length of string in cx mov ah, 0x07 ; normal attribute fixed in al

nextchar: mov al, [si] ; load next char of string mov [es:di], ax ; show this char on screen add di, 2 ; move to next screen location add si, 1 ; move to next char in string loop nextchar ; repeat the operation cx times

pop di pop si pop cx pop ax pop es pop bp ret 4

start: call clrscr ; call the clrscr subroutine

mov ax, message push ax ; push address of message push word [length] ; push message length call printstr ; call the printstr subroutine

mov ax, 0x4c00 ; terminate program int 0x

The string definition syntax discussed above is used to declare a

string “hello world” of 11 bytes and the length is stored in a separate

variable.

The code to clear the screen from the last example is written in the

form of a subroutine. Since the subroutine had no parameters, only

modified registers are saved and restored from the stack.

The standard subroutine format with parameters received via stack

and all registers saved and restored is used.

ES is initialized to point to the video memory via the AX register.

Two pointer registers are used; SI to point to the string and DI to

point to the top left location of the screen. CX is loaded with the

length of the string. Normal attribute of low intensity white on black

with no blinking is loaded in the AH register.

The next character from the string is loaded into AL. Now AH holds

the attribute and AL the ASCII code of the character. This pair is

DIV instruction available in the 8088 processor. There are two forms of the

DIV instruction. The first form divides a 32bit number in DX:AX by its 16bit

operand and stores the 16bit quotient in AX and the 16bit remainder in DX.

The second form divides a 16bit number in AX by its 8bit operand and stores

the 8bit quotient in AL and the 8bit remainder in AH. For example “DIV BL”

has an 8bit operand, so the implied dividend is 16bit and is stored in the AX

register and “DIV BX” has a 16bit operand, so the implied dividend is 32bit

and is therefore stored in the concatenation of the DX and AX registers. The

higher word is stored in DX and the lower word in AX.

If a large number is divided by a very small number it is possible that the

quotient is larger than the space provided for it in the implied destination. In

this case an interrupt is automatically generated and the program is usually

terminated as a result. This is called a divide overflow error; just like the

calculator shows an –E– when the result cannot be displayed. This interrupt

will be discussed later in the discussion of interrupts.

DIV (divide) performs an unsigned division of the accumulator (and its

extension) by the source operand. If the source operand is a byte, it is

divided into the two-byte dividend assumed to be in registers AL and AH. The

byte quotient is returned in AL, and the byte remainder is returned in AH. If

the source operand is a word, it is divided into the two-word dividend in

registers AX and DX. The word quotient is returned in AX, and the word

remainder is returned in DX. If the quotient exceeds the capacity of its

destination register (FF for byte source, FFFF for word source), as when

division by zero is attempted, a type 0 interrupt is generated, and the

quotient and remainder are undefined.

Number Printing Example

The next example introduces a subroutine that can print a number

received as its only argument at the top left of the screen using the algorithm

just discussed.

Example

; number printing algorithm [org 0x0100] jmp start

;;;;; COPY LINES 008-025 FROM EXAMPLE 6.2 (clrscr) ;;;;;

; subroutine to print a number at top left of screen ; takes the number to be printed as its parameter printnum: push bp mov bp, sp push es push ax push bx push cx push dx push di

mov ax, 0xb mov es, ax ; point es to video base mov ax, [bp+4] ; load number in ax mov bx, 10 ; use base 10 for division mov cx, 0 ; initialize count of digits

nextdigit: mov dx, 0 ; zero upper half of dividend div bx ; divide by 10 add dl, 0x30 ; convert digit into ascii value push dx ; save ascii value on stack inc cx ; increment count of values cmp ax, 0 ; is the quotient zero jnz nextdigit ; if no divide it again

mov di, 0 ; point di to top left column

nextpos: pop dx ; remove a digit from the stack mov dh, 0x07 ; use normal attribute mov [es:di], dx ; print char on screen add di, 2 ; move to next screen location loop nextpos ; repeat for all digits on stack

pop di pop dx pop cx pop bx pop ax pop es pop bp ret 2

start: call clrscr ; call the clrscr subroutine

mov ax, 4529 push ax ; place number on stack call printnum ; call the printnum subroutine

mov ax, 0x4c00 ; terminate program int 0x

026-033 The registers are saved as an essential practice. The only parameter

received is the number to be printed.

035-039 ES is initialized to video memory. AX holds the number to be

printed. BX is the desired base, and can be loaded from a parameter.

CX holds the number of digits pushed on the stack. This count is

initialized to zero, incremented with every digit pushed and is used

when the digits are popped one by one.

041-042 DX must be zeroed as our dividend is in AX and we want a 32bit

division. After the division AX holds the quotient and DX holds the

remainder. Actually the remainder is only in DL since the remainder

can be from 0 to 9.

043-045 The remainder is converted into its ASCII representation and saved

on the stack. The count of digits on the stack is incremented as well.

046-047 If the quotient is zero, all digits have been saved on the stack and if

it is non-zero, we have to repeat the process to print the next digit.

049 DI is initialized to point to the top left of the screen, called the cursor

home. If the screen location is to become a parameter, the value

loaded in DI will change.

051-053 A digit is popped off the stack, the attribute byte is appended to it

and it is displayed on the screen.

054-055 The next screen location is two bytes ahead so DI is incremented by

two. The process is repeated CX times which holds the number of

digits pushed on the stack.

057-064 We pop the registers pushed and “ret 2” to discard the only

parameter on the stack.

066-070 The main program clears the screen and calls the printnum

subroutine to print 4529 on the top left of the screen.

When the program is executed 4529 is printed on the top left of the screen.

This algorithm is versatile in that the base number can be changed and the

printing will be in the desired base. For example if “mov bx, 10” is changed to

“mov bx, 2” the output will be in binary as 001000110110001. Similarly

changing it to “mov bx, 8” outputs the number in octal as 10661. Printing it

in hexadecimal is a bit tricky, as the ASCII codes for A-F do not consecutively

start after the codes for 0-9. Inside the debugger observe the working of the

algorithm is just as described in the above illustration. The digits are