part iii: assembly language

27
ASSEMBLY LANGUAGE Ahmed M. Abed Teaching assistant – Islamic university of Gaza [email protected]

Upload: ahmed-m-abed

Post on 22-Nov-2014

403 views

Category:

Technology


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Part III: Assembly Language

ASSEMBLY LANGUAGEAhmed M. Abed

Teaching assistant – Islamic university of [email protected]

Page 2: Part III: Assembly Language

REGISTERS

Page 3: Part III: Assembly Language

GENERAL PURPOSE REGISTERS

Page 4: Part III: Assembly Language

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.

Page 5: Part III: Assembly Language

FIRST PROGRAM IN ASSEMBLY

TITLE A04ASM1 (EXE) Move and add operations

.STACK

.DATAFLDD DW 215FLDE DW 125FLDF DW ?

.CODEMAIN PROC

MOV AX,@DATA ;Set address of data

MOV DS,AX ; segment in DS MOV AX,FLDD ;Move 0215

to AXADD AX,FLDE ;Add

0125 to AXMOV FLDF,AX ;Store

sum in FLDFMOV AX,4C00H ;End

processingINT 21H

MAIN ENDP ;End of procedureEND MAIN ;End of program

Page 6: Part III: Assembly Language

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

Page 7: Part III: Assembly Language

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

Page 8: Part III: Assembly Language

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

Page 9: Part III: Assembly Language

EXAMPLE.model small.datamsg DB 'Hello, World'msg1 DB '$'.codemov ax,@DATAmov ds,axmov ah,9mov dx,offset msgint 21hmov ah,4chint 21h end

Page 10: Part III: Assembly Language

ANALYSIS OF THE EXAMPLEMODEL is a directive to tell the

assembler how to segment memory.There are six models of an assemblyprogram• Tiny : both code and data in

same64KB (one segment). Files with .comextensions.• 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, noarray may exceed 64KB• Large: Both data and code in

morethan one segment.• Flat: Protected mode. Uses

32-bit offsets for code and data

Page 11: Part III: Assembly Language

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 toput it in ax register.

mov ax, @datamov ds, ax

As note we can't put data in ds registerdirectly. 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

serviceslike9,8,2, and each one has special work.

mov ah,4chint 21h

The two statements to terminate theexecution of the program.

END is a directive to indicate the end of file

Page 12: Part III: Assembly Language

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 stringsReading characters and strings from the keyboardDisplaying text in colorOpening and closing filesReading data from filesWriting data to filesSetting and retrieving the system time and date

Page 13: Part III: Assembly Language

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.

Page 14: Part III: Assembly Language

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.

Page 15: Part III: Assembly Language

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.

Page 16: Part III: Assembly Language

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 ; terminate processmov al,0 ; return codeint 21h

; Same as:

.EXIT 0

Page 17: Part III: Assembly Language

CONT. 2-Function 02h

INT 21h Function 2Description

Write a single character to standard output and advance the cursor one column forward

Receives AH =2DL =character value

Returns Nothing

Sample call

mov ah, 2mov dl , 'A'int 21h

Page 18: Part III: Assembly Language

CONT. 3-Function 06hINT 21h Function 6Description

Write a character to standard output

Receives AH = 6DL = character value

Sample call

mov ah , 6mov dl , "A"int 21h

Notes Unlike other INT 21h functions, this one does not filter (interpret) ASCII control characters.

Page 19: Part III: Assembly Language

CONT. 4-Function 05hINT 21h Function 5Description

Write a single character to the printer

Receives AH = 5DL = character value

Returns Nothing

Sample call

mov ah , 5 ; select printer outputmov dl , "Z“ ; character to be printedint 21h ; call MS-DOS

Notes Unlike other INT 21h functions, this one does not filter (interpret) ASCII control characters.

Page 20: Part III: Assembly Language

CONT. 5-Function 09hINT 21h Function9Description

Write a $-terminated string to standard output

Receives AH = 9DS:DX = segment/offset of the string

Returns NothingSample call

.datastring BYTE "This is a string$".codemov ah , 9mov dx , OFFSET stringint 21h

Notes The string must be terminated by a dollar-sign character ($).

Page 21: Part III: Assembly Language

CONT. 6-Function 40hINT 21h Function 40hDescription

Write an array of bytes to a file or device

Receives AH = 40hBX = file or device handle (console 1)CX = number of bytes to writeDS:DX = address of array

Returns AX = number of bytes writtenSample call

message DB "Hello, world".codemov ah , 40hmov bx , 1mov cx , LENGTHOF messagemov dx , OFFSET messageint 21h

Page 22: Part III: Assembly Language

CONT. OF INT 21H FUNCTIONS

7-Function 01h

INT 21h Function 1Description

Read a single character from standard input

Receives AH = 1

Returns AX = character (ASCII code)Sample call

character (ASCII code)

NotesIf no character is present in the input buffer, the program waits. This function echoes the character to standard output.

Page 23: Part III: Assembly Language

DATE/TIME FUNCTIONS

8-Function 2AhINT 21h Function 2AhDescription

Get the system date

Receives AH = 2Ah

Returns CX = yearDH, DL = month, dayAL = day of week (Sunday 0, Monday 1, etc.)

Sample call

mov ah,2Ahint 21hmov year,cxmov month,dhmov day,dlmov dayOfWeek,al

Page 24: Part III: Assembly Language

CONT.

9-Function 2Bh

INT 21h Function 2BhDescription

Set the system date

Receives AH =2BhCX =yearDH =monthDL =day

Returns If the change was successful, AL 0; otherwise, AL FFh.

Sample call

mov ah,2Bhmov cx,yearmov dh,monthmov dl,dayint 21h

Page 25: Part III: Assembly Language

CONT.

9-Function 2ChINT 21h Function 2ChDescription

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,2Chint 21hmov hours,chmov minutes,clmov seconds,dh

Page 26: Part III: Assembly Language

CONT.

9-Function 2Dh

INT 21h Function 2DhDescription

Set the system time

Receives AH = 2DhCH = 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,2Dhmov ch,hoursmov cl,minutesmov dh,secondsint 21h

Page 27: Part III: Assembly Language

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 ; number of bytes mov dx,OFFSET message ; addr of buffer int 21h .EXIT main ENDP END main