4. SEGMENT REGISTERS
CS - points at the segment containing the current program.
DS - generally points at segment where variables are defined.
ES - extra segment register, it's up to a coder to define its usage.
SS - 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.
5. FIRST PROGRAM IN ASSEMBLY
.CODE
MAIN PROC
MOV AX,@DATA ;Set address of data
TITLE
A04ASM1 (EXE) Move and add operations
MOV
DS,AX
; segment in DS
.STACK
MOV
AX,FLDD
;Move 0215 to AX
.DATA
ADD
AX,FLDE
;Add 0125 to AX
FLDD
DW
215
MOV
FLDF,AX
FLDE
DW
125
MOV
AX,4C00H
FLDF
DW
?
INT
21H
;Store sum in FLDF
;End processing
MAIN ENDP
;End of procedure
END MAIN
;End of program
6. CONT.
TITLE line (optional)
Contains a brief heading of the program and the disk file name.
.STACK directive
Tells the assembler to define a runtime stack for the program
The size of the stack can be optionally specified by this directive
The runtime stack is required for procedure calls
7. CONT.
.DATA directive
Defines an area in memory for the program data
The program's variables should be defined under this directive
Assembler will allocate and initialize the storage of variables
.CODE directive
Defines the code section of a program containing instructions
Assembler will place the instructions in the code area in memory
8. CONT.
PROC and ENDP directives
Used to define procedures
As a convention, we will define main as the first procedure
Additional procedures can be defined after main
END directive
Marks the end of a program
Identifies the name (main) of the program’s startup procedure
9. EXAMPLE
.model small
.data
msg DB 'Hello, World'
msg1 DB '$'
.code
mov ax,@DATA
mov ds,ax
mov ah,9
mov dx,offset msg
int 21h
mov ah,4ch
int 21h
end
10. ANALYSIS OF THE EXAMPLE
MODEL
is a directive to tell the assembler
how to segment memory.
There are six models of an assembly
program
• Tiny : both code and data in same
64KB (one segment). Files with .com
extensions.
• Small: all data in one segment &
all code in one segment.
• Medium: all data in one segment,
but code in more than one
segment.
• Compact: Data in more than one
segment, all code in one segment, no
array may exceed 64KB
• Large: Both data and code in more
than one segment.
• Flat: Protected mode. Uses 32-bit
offsets for code and data
11. CONT.
.DATA
is a directive to put in data segment.
.CODE is a directive to put in code
segment.
is a directive to put in code segment.
@Data
is a default address of data segment to
put it in ax register.
mov ax, @data
mov ds, ax
As note we can't put data in ds register
directly. So we use intermediate register
(ax) as in the mov ds, ax
mov ah, 9
Put the service 9 in ah.
int 21h
(Interrupt 21 hexa), it has many services
like9,8,2, and each one has special
work.
mov ah,4ch
int 21h
The two statements to terminate the
execution of the program.
END
is a directive to indicate the end of file
12. SOFTWARE INTERRUPTS
A software interrupt is a call to an operating system procedure. Most of these procedures,
called interrupt handlers , provide input-output capability to application programs.
They are used for such tasks as the following:
Displaying characters and strings
Reading characters and strings from the keyboard
Displaying text in color
Opening and closing files
Reading data from files
Writing data to files
Setting and retrieving the system time and date
13. INT INSTRUCTION
instruction calls a system subroutine also known as an interrupt
handler.
Before the INT instruction executes, one or more parameters
must be inserted in registers.
A number identifying the particular procedure must be moved to the
AH register.
Depending on the function, other values may have to be passed to
the interrupt in registers.
The syntax is
INT number
Where number is an integer in the range 0 to FF hexadecimal.
14. COMMON INTERRUPTS
INT 10h Video Services
• Procedures that display routines that control the cursor position, write text
in color, scroll the screen, and display video graphics.
INT 16h Keyboard Services
• Procedures that read the keyboard and check its status.
INT 17h Printer Services.
• Procedures that initialize, print, and return the printer status.
INT 1Ah Time of Day.
• Procedure that gets the number of clock ticks since the machine was
turned on or sets the counter to a new value.
INT 1Ch User Timer Interrupt.
• An empty procedure that is executed 18.2 times per second.
15. CONT.
INT 21h MS-DOS Services.
• Procedures that provide input-output, file handling, and memory
management. Also known as MS-DOS function calls.
MS-DOS provides a lot of easy-to-use functions for displaying
text on the console.
There are about 200 different functions supported by this
interrupt, identified by a function number placed in the AH
register.
A number of functions require that the 32-bit address of an input
parameter be stored in the DS:DX registers. DS, the data
segment register, is usually set to your program’s data area.
16. MS-DOS FUNCTION CALLS (INT 21H)
1.
Function 4Ch
Ends the current process (program), returns an optional 8-bit
return code to the calling process.
A return code of 0 usually indicates successful completion.
mov ah,4Ch
mov al,0
int 21h
; Same as:
.EXIT 0
; terminate process
; return code
17. CONT.
2-Function 02h
INT 21h Function 2
Description
Write a single character to standard output and advance the
cursor one column forward
Receives
AH =2
DL =character value
Returns
Nothing
Sample call
mov ah, 2
mov dl , 'A'
int 21h
18. CONT.
3-Function 06h
INT 21h Function 6
Description
Write a character to standard output
Receives
AH = 6
DL = character value
Sample call
mov ah , 6
mov dl , "A"
int 21h
Notes
Unlike other INT 21h functions, this one does not filter
(interpret) ASCII control characters.
19. CONT.
4-Function 05h
INT 21h Function 5
Description
Write a single character to the printer
Receives
AH = 5
DL = character value
Returns
Nothing
Sample call
mov ah , 5
mov dl , "Z“
int 21h
Notes
Unlike other INT 21h functions, this one does not filter
(interpret) ASCII control characters.
; select printer output
; character to be printed
; call MS-DOS
20. CONT.
5-Function 09h
INT 21h Function9
Description
Write a $-terminated string to standard output
Receives
AH = 9
DS:DX = segment/offset of the string
Returns
Nothing
Sample call
.data
string BYTE "This is a string$"
.code
mov ah , 9
mov dx , OFFSET string
int 21h
Notes
The string must be terminated by a dollar-sign character ($).
21. CONT.
6-Function 40h
INT 21h Function 40h
Description
Write an array of bytes to a file or device
Receives
AH = 40h
BX = file or device handle (console 1)
CX = number of bytes to write
DS:DX = address of array
Returns
AX = number of bytes written
Sample call
message DB "Hello, world"
.code
mov ah , 40h
mov bx , 1
mov cx , LENGTHOF message
mov dx , OFFSET message
int 21h
22. CONT. OF INT 21H FUNCTIONS
7-Function 01h
INT 21h Function 1
Description
Read a single character from standard input
Receives
AH = 1
Returns
AX = character (ASCII code)
Sample call
character (ASCII code)
Notes
If no character is present in the input buffer, the program
waits. This function echoes the character to standard
output.
23. DATE/TIME FUNCTIONS
8-Function 2Ah
INT 21h Function 2Ah
Description
Get the system date
Receives
AH = 2Ah
Returns
CX = year
DH, DL = month, day
AL = day of week (Sunday 0, Monday 1, etc.)
Sample call
mov ah,2Ah
int 21h
mov year,cx
mov month,dh
mov day,dl
mov dayOfWeek,al
24. CONT.
9-Function 2Bh
INT 21h Function 2Bh
Description
Set the system date
Receives
AH =2Bh
CX =year
DH =month
DL =day
Returns
If the change was successful, AL 0; otherwise, AL FFh.
Sample call
mov ah,2Bh
mov cx,year
mov dh,month
mov dl,day
int 21h
25. CONT.
9-Function 2Ch
INT 21h Function 2Ch
Description
Get the system time
Receives
AH 2Ch
Returns
CH = hours (0 – 23)
CL = minutes (0 – 59)
DH = seconds (0 – 59)
DL = hundredths of seconds (usually not accurate)
Sample call
mov ah,2Ch
int 21h
mov hours,ch
mov minutes,cl
mov seconds,dh
26. CONT.
9-Function 2Dh
INT 21h Function 2Dh
Description
Set the system time
Receives
AH = 2Dh
CH = hours (0 – 23)
CL = minutes (0 – 59)
DH = seconds (0 – 59)
Returns
If the change was successful, AL 0; otherwise, AL FFh.
Sample call
mov ah,2Dh
mov ch,hours
mov cl,minutes
mov dh,seconds
int 21h
27. EXAMPLE
TITLE Hello World Program (Hello.asm)
.MODEL small
.STACK 100h
.data
message DB "Hello, world!",0dh,0ah
data_size= $ -offset message
.code
main PROC
mov ax,@data ; initialize DS
mov ds,ax
mov ah,40h ; write to file/device
mov bx,1 ; output handle
mov cx,data_size
mov dx,OFFSET message ; addr of buffer
int 21h
.EXIT
main ENDP
END main
; number of bytes
Notas do Editor
Some segments hold program instructions (code), others hold variables (data), and another segment named the stack segment holds local function variables and function parameters.
In 16-bit programs, the code generated by .EXIT ismov ah,4Ch ; terminate processint 21h