Top Banner
Control Structure vs. Assembly Language NASM
30

Control Structure vs. Assembly Language

Feb 03, 2016

Download

Documents

Chase

Control Structure vs. Assembly Language. NASM. If-then-else. If conditional then then_actions jump to endif else else_actions endif. Ex. If-Then-Else. If AL < 0xA then DL= AL + ’0’ Else DL = AL - 0xA +’A’ EndIf. MOVDL, AL CMP AL, 0xA JAETenUp ADDDL,’0’ JMPEndIf - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Control Structure vs. Assembly Language

Control Structurevs. Assembly Language

NASMNASM

Page 2: Control Structure vs. Assembly Language

If-then-else

If conditional then then_actions

jump to endif else else_actions endif

Condition

Then Actions Else Actions

T

F

Endif Label

Page 3: Control Structure vs. Assembly Language

Ex. If-Then-Else

If AL < 0xA then

DL= AL + ’0’ Else

DL = AL - 0xA +’A’

EndIf

MOV DL, AL CMP AL, 0xA JAE TenUp

ADD DL,’0’ JMP EndIf TenUp: ADD DL,’A’-0xA EndIf:

Page 4: Control Structure vs. Assembly Language

Repeat-Until

Loop at lease one time

Repeat

Statements

… Until Condition is True

Condition

Statements

T

F

Page 5: Control Structure vs. Assembly Language

Ex. Repeat-Until

BX <- 1 AX <- 1 CX <- 5 Repeat

BX <- BX + 2

AX <- BX * AX

CX <- CX – 1 Until CX=0

= 10395

MOV BX, 1 MOV AX, 1 MOV CX, 5 RLOOP: ADD BX, 2 MUL BX DEC CX JNZ RLOOP

= 0x289B

Page 6: Control Structure vs. Assembly Language

While loop

Check the condition before the loop

While Condition is true

Statements

Condition

Statements

TF

Page 7: Control Structure vs. Assembly Language

Ex. While-loop

BX <- 1 AX <- 1 CX <- 5 While CX > 0

BX <- BX + 2

AX <- BX * AX

CX <- CX – 1

= 10395

MOV BX, 1 MOV AX, 1 MOV CX, 5 WLOOP: CMP CX, 0 JE EndLOOP ADD BX, 2 MUL BX DEC CX JMP WLOOP EndLOOP:

Page 8: Control Structure vs. Assembly Language

For loop

Loop in the specific range with automatic decrease index.

Loop instruction with CX register.

Index <- Initial

Index <- Index + 1

Index <= Condtion

Statements

T F

Page 9: Control Structure vs. Assembly Language

Ex. For loop

“Loop” Instruction will automatically decrease CX and loop if CX is not zero

CX <- number of loop StartLoop:

Instructions

Loop StartLoop

MOV BX, 1 MOV AX, 1 MOV CX, 5 FLOOP: ADD BX, 2 MUL BX LOOP FLOOP

5 Loop

Page 10: Control Structure vs. Assembly Language

Frequently used routinesInt 21h

DOS Service routines

Page 11: Control Structure vs. Assembly Language

Int 21h’s format

Usage:

Reg AH is used as function number you wish to perform.

Ex. To call function 1 int 21h to get a character from keyboard.

First MOV AH, 01

INT 21H

Page 12: Control Structure vs. Assembly Language

Keyboard input with echo

Usage : AH <- 1

Return: AL -> character from standard input

device waits for keyboard input from STDIN and echoes to STDOUT

Returns 0 for extended keystroke, then function must be called again to return scan code

if Ctrl-Break is detected, INT 23 is executed

Page 13: Control Structure vs. Assembly Language

Display a character

Usage: AH <- 02 DL <- character to output

Return: Returns nothing

outputs character to STDOUT backspace is treated as non-destructive cursor

left if Ctrl-Break is detected, INT 23 is executed

Page 14: Control Structure vs. Assembly Language

Direct console input without echo

Usage: AH <- 07

Return: AL -> character from STDIN

waits for keyboard input until keystroke is ready character is not echoed to STDOUT returns 0 for extended keystroke, then function

must be called again to return scan code ignores Ctrl-Break and Ctrl-PrtSc - see INT 21,1

Page 15: Control Structure vs. Assembly Language

Console input without echo

Usage: AH <- 08

Return: AL -> character from STDIN

returns 0 for extended keystroke, then function must be called again to return scan code

waits for character from STDIN and returns data in AL

if Ctrl-Break is detected, INT 23 is executed

Page 16: Control Structure vs. Assembly Language

Display a string end with “$”

Usage: AH <- 09 DS:DX <- pointer to string ending in "$“

Returns: Returns nothing

outputs character string to STDOUT up to "$“ backspace is treated as non-destructive if Ctrl-Break is detected, INT 23 is executed

Page 17: Control Structure vs. Assembly Language

Read keyboard into a buffer

Usage AH <- 0A DS:DX <- pointer to input buffer of the format:

| max | count | BUFFER (N bytes)

| | `------ input buffer

| `------------ number of characters returned (byte)

`-------------- maximum number of characters to read (byte)

e.g. DB 0x5, 6 dup (?)

Page 18: Control Structure vs. Assembly Language

Read keyboard into a buffer

Returns since strings can be pre-loaded, it is recommended

that the default string be terminated with a CR N bytes of data are read from STDIN into buffer+2 max buffer size is 255, minimum buffer size is 1 byte chars up to and including a CR are placed into the

buffer beginning at byte 2; Byte 1 returns the number of chars placed into the

buffer (extended codes take 2 characters) DOS editing keys are active during this call INT 23 is called if Ctrl-Break or Ctrl-C detected

Page 19: Control Structure vs. Assembly Language

Clear Keyboard Buffer & Invoke Function

Usage: AH <- 0C AL <- 01, 06, 07, 08 or 0A

(INT 21 input functions) Return:

See return values from INT 21, AL where AL is 1, 6, 7, 8 or A

Main function is to clear the input buffer and call INT 21h with the specified function (in AL)

Page 20: Control Structure vs. Assembly Language

Create file using handle

Usage: AH <- 3C CX <- file attribute (see FILE ATTRIBUTES) DS:DX <- pointer to ASCIIZ path name

Return: CF = 0 if successful / =1 if error AX = files handle if successful = error code if failure (see DOS ERROR CODES)

if file already exists, it is truncated to zero bytes on opening

Page 21: Control Structure vs. Assembly Language

Open file using handle

Usage: AH <- 3D AL <- open access mode

00 read only, 01 write only, 02 read/write DS:DX <- pointer to an ASCIIZ file name

Return: AX = file handle if CF not set

= error code if CF set

(see DOS ERROR CODES)

Page 22: Control Structure vs. Assembly Language

Read File or Device Using Handle

Usage: AH <- 3F BX <- file handle CX <- number of bytes to read DS:DX <- pointer to read buffer

Return: AX = number of bytes read is CF not set = error code if

CF set (see DOS ERROR CODES) read specified number of bytes from file into buffer

DS:DX when AX is not equal to CX then a partial read

occurred due to end of file if AX is zero, no data was read, and EOF occurred

before read

Page 23: Control Structure vs. Assembly Language

Write To File or Device Using Handle

Usage: AH <- 40h BX <- file handle CX <- number of bytes to write, a zero value

truncates/extends the file to the current file position DS:DX <- pointer to write buffer

Return: AX = number of bytes written if CF not set = error code

if CF set (see DOS ERROR CODES) if AX is not equal to CX on return, a partial write

occurred this function can be used to truncate a file to the

current file position by writing zero bytes

Page 24: Control Structure vs. Assembly Language

Close File Using Handle

Usage: AH <- 3E BX <- file handle to close

Return: AX = error code if CF set (see DOS ERROR CODES)

if file is opened for update, file time and date stamp as well as file size are updated in the directory

handle is freed

Page 25: Control Structure vs. Assembly Language

Delete File

Usage: AH = 41h DS:DX = pointer to an ASCIIZ filename

Return: AX = error code if CF set (see DOS ERROR CODES)

marks first byte of file directory entry with E5 to indicate the file has been deleted.

The rest of the directory entry stays intact until reused. FAT pointers are returned to DOS

documented as not accepting wildcards in filename but actually does in several DOS versions

Page 26: Control Structure vs. Assembly Language

Rename File

Usage: AH <- 56h DS:DX <- pointer to old ASCIIZ path/filename ES:DI = pointer to new ASCIIZ path/filename

Return: AX = error code if CF set (see DOS ERROR CODES) supports full pathnames and allows renaming files

across directories and in DOS 3.x allows renaming subdirectories

does not support use of wildcards unless invoked from via INT 21,5D in which case error code 12h is returned

unpredictable result may occur if an opened file is renamed

Page 27: Control Structure vs. Assembly Language

Move File Pointer Using Handle

Usage: AH <- 42h AL <- origin of move: 00 = beginning of file plus offset

(SEEK_SET) 01 = current location plus offset (SEEK_CUR) 02 = end of file plus offset (SEEK_END)

BX <- file handle CX <- high order word of number of bytes to move DX <- low order word of number of bytes to move

Return: AX = error code if CF set (see DOS ERROR CODES) DX:AX = new pointer location if CF not set seeks to specified location in file

Page 28: Control Structure vs. Assembly Language

Ex. Show files’ contents org 100h jmp start

fh dw 0buf db 0

start:mov bl,[0x80] cmp bl,0 jz noparam

xor bh,bh add bl,0x81 ;set filename mov byte [bx],00;

mov ah,0x3d ;open mov al,0x00 mov dx,0x82 ;param int 21h jc error ;if flag set

mov [fh],ax;save file handle

next: mov bx,[fh];get file handle mov ah,0x3f;read file mov cx,1 ;1 byte mov dx,buf int 21h jc error ;check error cmp ax,0 ;ax=0 :: eof jz done

mov ah,2 ;display a char mov dl,[buf] int 21h jmp next

Page 29: Control Structure vs. Assembly Language

Ex. Show files’ contentsdone: mov ah,0x3e;close file int 21h int 20h

errorMsg db "There is an error$"error:mov ah,09 mov dx,errorMsg int 21h int 20h

noParamMsg db "Please enter a filename$"noparam:mov ah,09 mov dx,noParamMsg int 21h int 20h

Save -> show.asm

nasm show.asm -oshow.com

Usage: Show.com readme.txt

Page 30: Control Structure vs. Assembly Language

Q & A