You are on page 1of 17

Computer Organization & Assembly Language

Character Representation Input & Output a Character, Sample Programs

Character Representation

All data, characters must be coded in binary to be processed by the computer. ASCII:

American Standard Code for Information Interchange Most popular character encoding scheme. Uses 7 bit to code each character. 27 = 128 ASCII codes. Single character Code = One Byte [7 bits: char code, 8th bit set to zero] 32 to 126 ASCII codes: printable 0 to 31 and 127 ASCII codes: Control characters

Example

Show how character string RG 2z is stored in memory starting at address 0. Solution:


Address Character ASCII Code (HEX) ASCII Code (Binary) [Memory Contents]

0
1 2 3

R
G Space 2

52
47 20 32

0101 0010
0100 0111 0010 0000 0011 0010

7A

0111 1010

The Keyboard

Identifies a key by generating an ASCII code when the key is pressed. ASCII Keyboard Scan Code

How to Convert?

If a byte contains the ASCII code of an uppercase letter, what hex should be added to it to convert to lower case?

Solution: 20 h Example: A (41h) a (61 h)

If a byte contains the ASCII code of a decimal digit, What hex should be subtracted from the byte to convert it to the numerical form of the characters?

Solution: 30 h Example: 2 (32 h)

Sample Programs

Program 1: Echo
TITLE MY First Program .MODEL SMALL .STACK 100H .CODE ;display prompt MOV AH, 2 ;display character function MOV DL, '?' ;character is '?' INT 21H ;display it ;input a character MOV AH, 1 ;read character function INT 21H ;character in AL MOV BL, AL ;save it in BL

Contd..
;go to a new line MOV AH, 2 MOV DL, 0DH INT 21H MOV DL, 0AH INT 21H ;display character MOV DL, BL INT 21H ;return to DOS MOV AH, 4CH INT 21H

Program 2: Add
.DATA A DW 2 B DW 5 SUM DW ? .CODE ;add the numbers MOV AX, A ADD AX, B MOV SUM, AX ;exit to DOS MOV AX, 4C00H INT 21H

10

Program 3: The Hello World Program


title Hello World Program (hello.asm) ; This program displays Hello, world! .model small .stack 100h .data message db Hello, world!,0dh,0ah,$ .code main proc mov ax,@data mov ds,ax mov ah,9 mov dx,offset message int 21h mov ax,4C00h int 21h main endp end main

Irvine: Assembly Language for Intel-Based Computers (1999)

Sample Hello Program

program title (comment)

title Hello World Program

(hello.asm)

; This program displays Hello, world! .model small .stack 100h comment line memory model

set the stack size

Irvine: Assembly Language for Intel-Based Computers (1999)

Sample Hello Program


starts the data segment .data message db Hello, world!,0dh,0ah,$ starts the code segment .code main proc mov ax,@data sets DS to the offset of the mov ds,ax data segment mov ah,9 mov dx,offset message int 21h calls DOS display function 9

mov ax,4C00h int 21h main endp end main

halts program

Irvine: Assembly Language for Intel-Based Computers (1999)

Program 4: Lower To Upper case


TITLE Case Conversion Program .MODEL SMALL .STACK 100H .DATA CR EQU 0DH LF EQU 0AH MSG1 DB 'Enter a Lowe Case Letter: $' MSG2 DB 0DH, 0AH, 'In Upper Case It is: ' CHAR DB ?,'$ .CODE ;initialize DS MOV AX, @DATA ;get data segment MOV DS, AX ;initialize DS
14

Contd..
;print user prompt LEA DX, MSG1 ;get first message MOV AH, 9 ;display string function INT 21H ;display first message ;input a character and convert to upper case MOV AH, 1 ;read character function INT 21H ;read snall letter into AL SUB AL, 20H ;convert it into uppercase MOV CHAR, AL ;and store it ;display on the next line LEA DX, MSG2 ;get second message MOV AH, 9 ;display string function INT 21H ;display message and upper case letter in front ; 15

References

Chapter 2 & 4, Ytha Yu and Charles Marut, Assembly Language Programming and Organization of IBM PC

17

You might also like