kip irvine: assembly language for intel-based computers overview stack operations (push and pop)...

24
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) • Procedures Procedure Parameters Software Interrupts MS-DOS (INT 21h) Function Calls BIOS Keyboard Input (INT 16h) BIOS Video Control (INT 10h) • Recursion

Post on 20-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Overview

• Stack Operations (PUSH and POP)• Procedures• Procedure Parameters• Software Interrupts• MS-DOS (INT 21h) Function Calls• BIOS Keyboard Input (INT 16h)• BIOS Video Control (INT 10h)• Recursion

Page 2: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

0006 0006

00A5

SP

SP

(low memory) (low memory)

(high memory) (high memory)

BEFORE AFTER

push 0006h

push 00A5h

PUSH Instruction

Page 3: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

0006

00A5

0001

0002 SP

(low memory)

(high memory)

New Contents ofthe stack after

pushing 0001 and0002:

After pushing 0001 and 0002

Page 4: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

BEFORE AFTER

0006

00A5

0001

0002 SP

(low memory)

(high memory)

0006

00A5

0001 SP

(low memory)

(high memory)

Before and After Popping from the Stack

pop AX

; now, AX=0002

Page 5: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Uses of the Stack

• Save and restore registers• Save the return address when a CALL instruction is

executed• Push parameters on the stack before calling a

subroutine• Create local variables inside a procedure

A procedure's stack frame includes passed

parameters, the return address, and local variables.

Page 6: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

main proc mov ax,@data mov ds,ax call MySub mov ax,4c00h ; returns to here int 21hmain endp

MySub proc . ; control transfers here . retMySub endp

Example: Calling a Procedure

Page 7: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

main proc000A call sub1 000C mov ax,... . main endp

sub1 proc . call sub2 0050 ret sub1 endp

sub2 proc . call sub3 0060 ret sub2 endp

sub3 proc . . ret sub3 endp

Nested Procedure Calls (1)

Page 8: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Nested Procedure Calls (2)

000C

0050

0060 SP

(low memory)

(high memory)

Ret addr of first procedure call

Ret addr of second procedure call

Ret addr of third procedure call

Page 9: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

main proc . call subroutine1 .subroutine1 proc .main endp . . retsubroutine1 endp

Avoid Overlapping Procedures!

Page 10: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

title Procedure Demonstration (SUBS.ASM)

; This program calls two procedures: one for ; keyboard input, another to add the elements ; in an array of integers.

.model small

.stack 100h

.datachar db ?sum dw ? array dw 100h,200h,300h,400h,500harray_size = ($array)/(TYPE array)

; more...

Procedure Calls (1)

Page 11: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

.codemain proc mov ax,@data ; set up the DS register mov ds,ax

call inputChar ; input char into AL mov char,AL ; store in a variable

; Prepare to call the calcSum procedure.

mov bx,offset array ; BX points to array mov cx,array_size ; CX = array count call calcSum ; calculate sum mov sum,ax ; store in a variable

mov ax,4C00h ; return to DOS int 21hmain endp

Procedure Calls (2)

Page 12: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

; input character from keyboard

inputChar proc mov ah,1 ; DOS function #1: char input int 21h ; call DOS to do the work retinputChar endp

; more...

Procedure Calls (3)

Page 13: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

; Calculate the sum of an array of integers. ; Input: BX points to the array and CX contains; the array size. Returns the SUM in AX.

calcSum proc push bx ; save BX, CX push cx mov ax,0CS1: add ax,[bx] add bx,2 ; point to next integer loop CS1 ; repeat for array size pop cx ; restore BX, CX pop bx ret ; sum stored in AXcalcSum endp

Procedure Calls (4)

Page 14: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

0000

0009

0000

00090009 pushedon the stack

0009 poppedinto IP

STACK STACK

Calling a NEAR Procedure

main proc

0006: call sub1

0009: inc ax

.

main endp

sub1 proc

0080: mov ax,1

.

ret

sub1 endp

Page 15: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

0000

2FC0

0009

0000

2FC0

0009

CS and IPare pushedon thestack.

The returnsegment andoffset values arepopped into CSand IP.

STACK STACK

Calling a FAR Procedure

main proc2FC0:0006: call far ptr sub12FC0:0009: inc ax . . main endp

sub1 proc3AB6:0080: mov ax,1 . ret sub1 endp sub1 endp

Page 16: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Preserving Local Registers (1)

Writeint proc push cx ; save registers that will change push bx push si . . pop si ; restore the same registers pop bx ; (in reverse order) pop cx retWriteint endp

It is common practice to save and restore any registers that a procedure plans to modify.

Page 17: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Preserving Local Registers (2)

main proc ... mov cx,LIST_COUNT mov bx,DECIMAL_RADIX mov si,offset aList L1: mov ax,[si] call Writeint add si,2 Loop L1 ...main endp

What would happen to the following program if Writeint did not preserve CX,BX, and SI?

Page 18: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Interrupts

• Hardware interrupts – occur as a response to a hardware device– routed through the Intel 8259 Interrupt Controller

• Software interrupts– calls to operating system functions, located in

BIOS and resident portion of DOS– activated by the INT instruction

Page 19: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Interrupt Vectoring Process

mov...int 10hadd...

F000:F0653069 F000:AB62

return to callingprogram

F000:F065 F066 F067 F068 . .

sti cld push es . . . IRET

1 2 3

Calling program

(entry for INT 10)

Interrupt Vector Table

Interrupt Handler

4

Page 20: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

INT Instruction

• The INT instruction is always followed by a hexadecimal number that identifies its type

• Common examples:– INT 10h - video BIOS– INT 14h - Serial I/O– INT 16h - keyboard BIOS– INT 17h - printer services– INT 1Ah - Time of day– INT 1Ch - User timer– INT 21h - DOS services

Page 21: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

DOS Function Calls (INT 21h)

• The INT 21h instruction activates a DOS function call

• The function number (0-255) is placed in the AH register before invoking INT 21h

• Some functions require that you assign values to certain registers before invoking INT 21h

• Some functions return values in registers

Page 22: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

Simple Console I/O

mov ah,1 ; single character input int 21h mov ah,2 ; single character output mov dl,'A' int 21h mov ah,9 ; string output mov dx,offset message int 21h

Page 23: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

• 01h Filtered Input With Echo

• 06h Direct Input Without Waiting

• 07h Direct Input, No Ctrl-Break

• 08h Direct Input with Ctrl-Break

• 0Ah Buffered Input

• 0Bh Get Input Status

• 0Ch Clear Input Buffer, Invoke Input Function

• 3Fh Read From File or Device

INT 21h: Standard Input

Page 24: Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS

Kip Irvine: Assembly Language for Intel-Based Computers

DOS Function Number 1 6 7 8

Waits for keystroke? Y N Y Y

Echoes character? Y N N N

Ctrl-Break recognized? Y N N Y

Filters control characters? Y N N N

Comparison of Standard Input