Upload
archer
View
52
Download
4
Tags:
Embed Size (px)
DESCRIPTION
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT Instruction. Program Structure. Assembly language programs consists of the following program segments: Code. Data. - PowerPoint PPT Presentation
Citation preview
Assembly Language
Lecture 2
Lecture Outline
• Program Structure• Memory models• Data Segment• Stack Segment• Code Segment
• Input and Output Instructions• INT Instruction
Program Structure
• Assembly language programs consists of the following program segments:
• Code.• Data.• Stack.
• Each program segment is translated into a memory segment by the assembler.
Program Structure - Memory Models
• The size of code and data a program can have is determined by specifying a memory model using the .MODEL directive.
• Syntax: .MODEL memory_model
Model DescriptionSMALL code in 1 segment data in 1 segmentMEDIUM code > 1 segment data in 1 segmentCOMPACT code in 1 segment data > 1 segmentLARGE code > 1 segment data > 1 segment
no array larger than 64k bytesHUGE code > 1 segment data > 1 segment
arrays may be larger than 64k bytes
Program Structure - Memory Models
• The appropriate model is SMALL, unless there is a lot of code or data.
• .MODEL directive should come before segment definitions.•A segment is 216 (64 k)
Program Structure - Stack Segment
• The purpose of the stack segment declaration is to set aside a block of memory (the stack area) to store the stack.
• The stack area should be big enough to contain the stack at its maximum size.
• Syntax: .STACK size ; where size is an optional number that specifies ; the stack area size in bytes.
• Example: .STACK 100H ; sets aside 100H bytes for the stack area.
; (reasonable size for most applications).
• If size is omitted, 1KB is set aside for the stack area.
Program Structure - Data Segment
• A program’s data segment contains all the variable definitions.
• Constant definitions are often made here as well. (they may be placed elsewhere in the program since no memory allocation is involved).
• To declare a data segment, we use the directive .DATA, followed by variable and constant declarations.
• Example: .DATA WORD1 DW 2 MSG DB ‘this is a message’
Program Structure - Code Segment
•The code segment contains a program’s instructions.
• Syntax: .CODE name ; where name is an optional name of segment. • There is no need for a name in a SMALL program, However, the assembler will generate an error.
• Inside a code segment, instructions are organized as procedures.
Program Structure - Code Segment
• The simplest procedure definition is: name PROC ; name: is the name of the procedure. ; body of the procedure ; PROC & ENDP: are pseudo-ops that name ENDP ; delineate the procedure
• Example of a code segment definition: .CODE MAIN PROC ; main procedure instructions MAIN ENDP ; other procedures go here
Program Structure - A General Form of a .SMALL model program
.MODEL SMALL
.STACK 100H
.DATA; data definitions go here.CODEMAIN PROC; instructions go hereMAIN ENDP; other procedures go hereEND MAIN
Input and Output Instructions
• There are two categories of I/O service routines:• The Basic Input/Output System (BIOS) routines.• The DOS routines.
INT Instruction
• To invoke a DOS or BIOS routine, the INT (interrupt) instruction is used.
• Format: INT interrupt_number
where interrupt_number is a number that specifies a routine.
INT 21h
• INT 21h may be used to invoke a large number of DOS functions.
• A particular function is requested by placing a function number in the AH register and invoking INT 21h.
• Some of the functions are:
• INT21h functions expect input values to be in certain registers and return output values in other registers.
Function number Routine1 single-key input2 single-character output9 character string output
INT 21h
• To invoke the routine, the following instructions should be executed: MOV AH,1 ; input key function INT 21H ; ASCII code in AL
Function 1: Single-Key InputInput: AH = 1Output: AL = ASCII code if character key is pressed = 0 if non-character key is pressed
INT 21h
• To invoke the routine, the following instructions should be executed: MOV AH, 2 ; display character function MOV DL, '?' ; character is '?' (or any other character) INT 21H ; display character
Function 2: Display a character or execute a control functionInput: AH = 2
DL = ASCII code of the characterOutput AL = ASCII code of the character
INT 21h
• Function 2 may be used to perform control functions.
• If DL contains the ASCII code of a control character, INT 21h causes the control function to be performed.
• The principal control characters are :
ASCII code (Hex) Symbol Function07H BEL beep (sounds a tone)08H BS backspace09H HT tab0AH LF line feed (new line)0DH CR carriage return (start of current line)
INT 21h
• To invoke the routine, the following instructions should be executed: MOV AX, @DATA MOV DS, AX MOV AH, 9 ; display string function LEA DX, MSG ; get message (Load Effective Address) INT 21H ; display string
Function 9: Display a stringInput: AH = 9 DX = offset address of string.
The string must end with a '$' character
A program containing a data segment should begins with these two instructions
INT 21h
• To invoke the routine, the following instructions should be executed: MOV AH, 4CH ; DOS exit function INT 21H ; exit to DOS
Function 4CH: Returning control to DOSInput: AH = 4CH
TITLE P1: ECHO PROGRAM .MODEL SMALL.STACK 100H.CODEMAIN PROC; display prompt
MOV AH,2 ; display character functionMOV DL,'?' ; character is '?'INT 21H ; display it
; input a characterMOV AH,1 ; read character functionINT 21H ; character in ALMOV BL,AL ; save it in BL
; go to new lineMOV AH,2 ; display character functionMOV DL, 0DH ; carriage returnINT 21H ; execute carriage returnMOV DL, 0AH ; line feedINT 21H ; execute line feed
; display characterMOV DL, BL ; retrieve characterINT 21H ; and display it
; return to DOSMOV AH, 4CH ; DOS exit functionINT 21H ; exit to DOS
MAIN ENDP END MAIN
Echo Program
Print String Program
TITLE P2: PRINT STRING PROGRAM.MODEL SMALL.STACK 100H.DATAMSG DB 'HELLO!$'.CODEMAIN PROC; initialize DS
MOV AX,@DATA; get data segmentMOV DS,AX ; initialize DS
; display messageLEA DX,MSG ; get messageMOV AH,9 ; display string functionINT 21H ; display message
; return to DOSMOV AH, 4CH ; DOS exit functionINT 21H ; exit to DOS
MAIN ENDPEND MAIN