Assembly language lecture7

advertisement

Stack instructions

push general-purpose register/segment register/ memory/number

- store the operand on the top of the stack:

SP := SP – 2(4), [SP] := operand

The operand must be of type word or dword. pop general-purpose reg./segment reg. (except CS)/memory

- retrieve the operand from the stack: operand := [SP], SP := SP + 2(4)

The operand must be of type word or dword. pusha

- store all 16-bit general-purpose registers on the stack in the order: AX, CX, DX, BX, SP (contents before pusha ), BP, SI,

DI. popa

- retrieve all 16-bit general-purpose registers from the stack in the order: DI, SI, BP, the next word is ignored, BX, DX, CX,

AX.

Assembly Language 7/ 1

pushad

- store all 32-bit general-purpose registers on the stack in the order: EAX, ECX, EDX, EBX, ESP (contents before pushad ),

EBP, ESI, EDI. popad

- retrieve all 16-bit general-purpose registers from the stack in the order: EDI, ESI, EBP, the next two words are ignored,

EBX, EDX, ECX, EAX. pushf

- store on the stack the 16-bit flags register FLAGS. pushfd

- store on the stack the 32-bit flags register EFLAGS. popf

- retrieve register FLAGS. popfd

- retrieve register EFLAGS.

Assembly Language 7/ 2

Instructions popf and popfd change flags, the other do not.

Instructions push number , pusha, popa, pushad, popad, pushfd and popfd are from the extended instruction set.

Assembly Language 7/ 3

Procedures

Declaration: name PROC [ near/far ] the body of the procedure ret ; return to the calling program name ENDP

If the type specification near / far is missing, the default procedure type is used depending on the way the segments are defined:

• standard segment directives ( SEGMENT ) => near

• simplified segment directives ( .MODEL

): o memory model tiny, small, compact, flat => near o memory model medium, large, huge => far

Procedure call

Direct: call name of the procedure

Indirect: call register/memory

– store the return address on the stack and jump to the first instruction of the procedure.

The return address is the current contents of IP, i.e. the offset of the instruction following the call instruction.

Assembly Language 7/ 4

Call may be:

• near – the procedure is in the same segment as the call instruction

• far – the procedure is in another segment; first CS is stored on the stack, then IP call is compiled in the same way as jmp .

Forward far call: call far ptr name of the procedure ret

- retrieve the return address from the stack and store it to IP

(resp. CS:IP if the procedure if of type far).

.MODEL small

.STACK 100h

.CODE

Nothing PROC cs:0000 90 nop cs:0001 C3 ret

Nothing ENDP cs:0002 E8 FB FF Start: call Nothing cs:0005 B4 4C mov ah,4Ch cs:0007 CD 21 int 21h

END Start displacement = -5

Assembly Language 7/ 5

At the beginning of the main program:

IP 0002 00FE

Stack

?

?

SP 0100 0100 ?

?

After call Nothing :

IP

SP

0000

00FE

00FE

0100

05

00

?

?

Assembly Language 7/ 6

After nop :

IP

SP

After ret :

IP

SP

0001

00FE

0005

0100

00FE

0100

05

00

?

?

00FE

0100 ?

?

05

00

Assembly Language 7/ 7

Procedures with parameters

Parameters may be passed:

• in general-purpose registers – in pure assembly language programs

• in the stack: o in high level language programs o if several parameters are required

Passing parameters in the stack

Before the procedure call, the parameters are pushed on the stack. In the procedure, the parameters are accessed using the indirect addressing mode with the base register BP.

Example

Write a procedure that adds a given number to each element of an array of type byte. Parameters of the procedure include:

• the address of the array

• the length of the array (in bytes)

• the number to be added

.MODEL small

.STACK 100h

.DATA

Array DB 0,1,2,3,4

ArrayLength DW $-Array

Number EQU 1

.CODE

.386

Assembly Language 7/ 8

ArrayAdd PROC

mov bp,sp

mov bx,[bp+6]

mov cx,[bp+4]

mov al,[bp+2]

Next: add [bx],al

inc bx

loop Next

ret 6; retrieve the return address and add 6 to SP to clear the parameters

ArrayAdd ENDP

Start: mov ax,@data

mov ds,ax

push offset Array

push ArrayLength

push Number

call ArrayAdd

Stop: mov ax,4C00h

int 21h

END Start Stack after call ArrayAdd :

SP return address

SP + 2 Number

SP + 4 ArrayLength

SP + 6 offset Array

0100 ?

Assembly Language 7/ 9

Formal parameters

The ARG directive allows you to access parameters in a procedure by their symbolic names instead of indirect addresses.

Advantages:

• more readable source code of the procedure

• you need not remember the offsets of the parameters relative to SP.

Syntax:

ARG parameter1 [, parameter2] … [= symbol] parameters in the increasing order from SP symbolic constant – set by the compiler to the total number of bytes that the formal parameters occupy in the stack

You may define the type and the number of entries for each formal parameter, e.g.

ARG Vector:dword:5 defines formal parameter Vector (of type dword), which consists of 5 doublewords.

The default type is word.

The default number of entries is 1, except type byte where is

2.

Assembly Language 7/ 10

Procedure with formal parameters the same as paNumber EQU byte ptr [bp+4]

ArrayAdd PROC

ARG paNumber:byte,paArrayLength,paOffset =

Param

push bp; preserve BP from the main program mov bp,sp mov bx,paOffset mov cx,paArrayLength mov al,paNumber

Next: add [bx],al inc bx loop Next

pop bp; restore BP ret Param

ArrayAdd ENDP

Stack after mov bp,sp:

SP caller’s BP

BP + 2 return address

BP + 4 Number

BP + 6 ArrayLength

BP + 8 offset Array

0100 ?

Assembly Language 7/ 11

Local variables

Local variables are allocated in the stack above the return address and the BP from the main program.

Syntax:

LOCAL variable1 [, variable2] … [= symbol] symbolic constant – set by the compiler to the total number of bytes that the local variables occupy in the stack

Example

Modify the procedure ArrayAdd so that to sum all elements of the Array . Hold the sum in a local variable Sum .

ArrayAdd PROC

ARG paNumber:byte,paArrayLength,paOffset =

Param

LOCAL Sum = LocalSize

push bp; preserve BP from the main program mov bp,sp

sub sp,LocalSize; allocate space for local variables mov bx,paOffset mov cx,paArrayLength mov al,paNumber

Assembly Language 7/ 12

mov Sum,0

Next: add [bx],al movzx dx,[bx] add Sum,dx

inc bx

loop Next

mov sp,bp; restore SP

pop bp; restore BP ret Param

ArrayAdd ENDP

Stack after sub sp,LocalSize :

SP locations that can be used in the procedure

Sum

BP caller’s BP

BP + 2 return address

BP + 4 Number

BP + 6 ArrayLength

BP + 8 offset Array

0100 ?

Assembly Language 7/ 13

Pascal convention in procedure declaration

.CODE

ArrayAdd PROC Pascal,paOffset,paArrayLength, paNumber:byte

LOCAL Sum

mov bx,paOffset mov cx,paArrayLength mov al,paNumber mov Sum,0

Next: add [bx],al movzx dx,[bx] add Sum,dx inc bx loop Next ret

ArrayAdd ENDP

Pascal convention in procedure call

Start: mov ax,@data

mov ds,ax

call ArrayAdd Pascal, offset Array,ArrayLength,Number

Stop: mov ax,4C00h actual parameters

int 21h

END Start

Assembly Language 7/ 14

Calling conventions

Programming language

Parameters are pushed

Parameters are discarded from the stack by

Pascal

Basic

Fortran from the left to the right procedure

(executing the instruction ret n

C

Prolog from the right to the left calling program stdcall

(used in 32-bit applications calling

Windows services) from the right to the left procedure

)

Assembly Language 7/ 15

Download