Download Set 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ICS312 Set 4
Program Structure
Outline for a SMALL Model Program
Note the quiz at the next lecture will be to reproduce this slide
.MODEL SMALL
.586
; allows Pentium instructions to be used
.STACK 100H
.DATA
; put data definitions here
.CODE
MAIN PROC
; put instructions for MAIN procedure here
MAIN ENDP
; include additional procedures after MAIN procedure
SUB1 PROC
...
SUB1 ENDP
END MAIN
; END directive ends program
; Label used with END directive matches
;
label of first instruction to be executed in the
;
code segment. (Any legal label can be used.)
Program Structure (1)
Memory Model
Use SMALL memory model for assignments --- creates 1
code segment, 1 data segment and 1 stack segment.
.MODEL SMALL
Processor Directive
To use the Pentium instruction set for PCs, code the 586
processor directive immediately after the .MODEL
directive
Example
.MODEL SMALL
.586
Program Structure (2)
Stack Segment
•Set the stack size to 100h bytes, using
.STACK 100h
•If no stack size is set, the default size is
1000h bytes. A smaller stack size (100h bytes)
is more than enough for programs in this
course.
Program Structure (3)
Data Segment
Define all variables in the data segment, so the memory allocated for
them will be in the program's data area.
Different ways data can be coded
Numbers (all converted into binary)
binary numbers
with suffix “b”, eg: 0101101b
hexadecimal numbers
with suffix “h”, eg: 3Fh
decimal number
Characters
Use single or double quotes eg: ‘HI THERE’ or “HI THERE”
Variables
Variables require that memory space be allocated for storing values of a
specified data type. In assembler programming, a variable's data type
determines the amount of space needed: one byte, two bytes, 4 bytes, etc.
Byte Variables
declared using DB pseudo-opcode
Word Variables (two bytes)
declared using DW pseudo-opcode
Double Word Variables (four bytes)
declared using DD pseudo-opcode
Program Structure (4)
•Example of a Data Segment
.DATA
CRLF
PROMPT
VAR1
ARRAY
EQU
DB
DB
DW
0DH, 0AH
'Enter a digit between 0 and 9$’
?
1234h, 23h, 0FF54h
Program Structure (5)
Code Segment
•The code segment contains all the source code for the program.
•Starts with the directive:
.CODE
•Use procedures to organize the program code (example below)
Assembly Language Syntax
•General form of assembly language Instructions:
[name]
[operation code]
[operand(s)]
[;comment]
•Types of Assembly Language Statements:
Instructions - part of the machine language instruction set.
Instructions will be converted into machine code by the
assembler translator.
Directives/Pseudo-Operations - directives are NOT part of the
machine language instruction set and will be processed by the
assembler translator at assembly time.
Input and Output Instructions
Some basic INT 21H Input/Output Functions:
Functio
Description
n
1
2
Register Setup
Input one
character from
keyboard with
echo to screen
AH = 1
Output one
character to
screen
AH = 2 DL = ASCII
code or control
character code
Result
AL = ASCII code of the character, if char key
pressed, or
AL = 0, if non-char key pressed eg:the up
arrow
Contents of DL are written on
screen
Examples shown above:
•Read a character from the keyboard
•Display a character on the monitor
ASCII codes for control characters
ASCII code
Symbol
Function
07h
BEL
beep
08h
BS
backspace
09h
HT
horizontal tab
0Ah
LF
line feed
0Dh
CR
carriage return
Displaying a String
Function: Description:
Input:
AH = 9
9
Display a
string
DX = offset address of string.
String must end with '$'
Example
Write a program that displays a message on the screen.
•Define the message in the data segment of the program.
•Use LEA Instruction to put the address of the message
in the DX register.
•Display the Message.
TITLE SAMPLE PROGRAM
.MODEL
SMALL
.586
.STACK
100H
.DATA
MES
DB ‘This is a sample program$’
.CODE
The portions in the
MAIN
PROC
blue boxes will occur
MOV AX, @DATA
MOV DS, AX
in every main
MOV AH, 9
program exactly as
LEA DX, MES
INT 21H
shown, except that
MOV AH, 4CH
the label “main” (in its
INT 21H
MAIN
ENDP
three ocurrences) can
END MAIN
be replaced by any
other label.
HMW: Your First Program
•Write a program to read a character from the keyboard
and display it at the beginning of the next line.
Program outline:
•Display a question mark to prompt for input.
•Read a character
•Display the character on the next line
•save the character in another register
•move the cursor to the next line (execute carriage
return and line feed)
•display the character
Creating and running your program
Create the source file in text format using any
word processor. Give it a name with “asm”
as suffix, e.g: first.asm
To use the word processor available in dos,
employ: edit first.asm
Refer to the instructions on downloading
Masm 6.15 on the web page. Assemble and
run first.asm as illustrated there, i.e:
ml first.asm
first
Notes:
1.
For the purpose of readibility, employ
function 2 to output a single character, and
function 9 to output a string of more than
one character
2.
You can’t assume that the int 21h
subroutines will preserve the values of al,
ax or eax. So your code for a new line
might wipe out any character that you have
just read into al.
Textbook Reading (Jones):
Chapter 2 Assembler Overview
& first part of Chapter 3