Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif.

Similar presentations


Presentation on theme: "Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif."— Presentation transcript:

1 Control Structure vs. Assembly Language NASM

2 If-then-else If conditional then then_actions jump to endif else else_actions endif

3 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 TenUp: ADDDL,’A’-0xA EndIf:

4 Repeat-Until Loop at lease one time Repeat Statements … Until Condition is True

5 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

6 While loop Check the condition before the loop While Condition is true Statements …

7 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:

8 For loop Loop in the specific range with automatic decrease index. Loop instruction with CX register.

9 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

10 Frequently used routines Int 21h DOS Service routines

11 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. FirstMOV AH, 01 INT 21H

12 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 executedCtrl-BreakINT 23

13 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 executedCtrl-BreakINT 23

14 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

15 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

16 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 executedCtrl-BreakINT 23

17 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 (?)

18 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

19 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)

20 Create file using handle Usage: AH <- 3C CX <- file attribute (see FILE ATTRIBUTES)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)DOS ERROR CODES if file already exists, it is truncated to zero bytes on opening

21 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)DOS ERROR CODES

22 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 ) 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

23 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 ) 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

24 Close File Using Handle Usage: AH <- 3E BX <- file handle to close Return: AX = error code if CF set (see DOS ERROR CODES)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

25 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 FAT documented as not accepting wildcards in filename but actually does in several DOS versions

26 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)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 returnedINT 21,5D unpredictable result may occur if an opened file is renamed

27 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)DOS ERROR CODES DX:AX = new pointer location if CF not set seeks to specified location in file

28 Ex. Show files’ contents org 100h jmp start fh dw 0 buf 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

29 Ex. Show files’ contents done: 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

30 Q & A


Download ppt "Control Structure vs. Assembly Language NASM. If-then-else If conditional then then_actions jump to endif else else_actions endif."

Similar presentations


Ads by Google