+ All Categories
Home > Documents > Today's topics

Today's topics

Date post: 27-Jan-2016
Category:
Upload: mimir
View: 21 times
Download: 0 times
Share this document with a friend
Description:
Today's topics. Multi-dimensional arrays String processing Macros. 2-dimensional arrays. Example declaration: Matrix DWORD 5 DUP(3 DUP(?)) ; 15 elements Row major order Row index first (5 rows, 3 columns) i.e., 5 rows, 3 elements per row In HLLs, reference Matrix[0][2] - PowerPoint PPT Presentation
Popular Tags:
29
Today's topics Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros
Transcript
Page 1: Today's topics

Today's topicsToday's topics

Multi-dimensional arraysMulti-dimensional arrays String processingString processing MacrosMacros

Page 2: Today's topics

2-dimensional arrays2-dimensional arrays Example declaration:Example declaration:Matrix DWORD Matrix DWORD 5 DUP(3 DUP(?))5 DUP(3 DUP(?)) ; 15 ; 15

elementselements

Row major orderRow major order Row index first (5 rows, 3 columns)Row index first (5 rows, 3 columns)

i.e., 5 rows, 3 elements per rowi.e., 5 rows, 3 elements per row In HLLs, reference Matrix[0][2] In HLLs, reference Matrix[0][2]

Last element in first row … etc.Last element in first row … etc. In assembly language, it’s just a set In assembly language, it’s just a set

of contiguous memory locationsof contiguous memory locations

Page 3: Today's topics

2-dimensional arrays2-dimensional arrays An element’s address is calculated as An element’s address is calculated as

the base address plus an offsetthe base address plus an offsetBaseAddress + elementSize * [(row# * elementsPerRow) + BaseAddress + elementSize * [(row# * elementsPerRow) +

column#]column#]

Example: Suppose Example: Suppose MatrixMatrix is at is at address 20A0address 20A0hh The The addressaddress of Matrix[3][1] is of Matrix[3][1] is

20A020A0hh + 4 * [(3 * 3) + 1] = 20C8 + 4 * [(3 * 3) + 1] = 20C8hhSize of DWORD Row

index

Elements per row

Column index

Base address

4*[(3*3)+1]= 4*[9+1]= 4 * Ah= 28h

Page 4: Today's topics

Matrix: element addresses Matrix: element addresses (hexadecimal)(hexadecimal)

Matrix 0 1 2

0 20A0 20A4 20A8

1 20AC 20B0 20B4

2 20B8 20BC 20C0

3 20C4 20C8 20CC

4 20D0 20D4 20D8

Page 5: Today's topics

LocalLocal Directive Directive A A local variablelocal variable is created, used, and is created, used, and

destroyed within a single proceduredestroyed within a single procedure The The LOCALLOCAL directive declares a list of directive declares a list of

local variableslocal variables immediately follows the immediately follows the PROCPROC directive directive each variable is assigned a typeeach variable is assigned a type

Syntax:Syntax:LOCAL LOCAL varlistvarlist

Example:Example:

MySub PROCLOCAL var1:BYTE, var2:WORD,

var3:SDWORD

Page 6: Today's topics

Local variablesLocal variables

MASM keeps local variables on the MASM keeps local variables on the system stacksystem stack Generates replacement code for the Generates replacement code for the

LOCALLOCAL directive directive

Page 7: Today's topics

Sort PROCLOCAL temp:DWORD, SwapFlag:BYTE; procedure coderet

Sort ENDP

Sort PROCpush ebpmov ebp,espsub esp,08h ;subtract 8 from ESP; to create space for local variables; procedure code

pop ebpret

Sort ENDP

MASM generates the following code:

Page 8: Today's topics

LEA InstructionLEA Instruction• LEA means LEA means Load Effective AddressLoad Effective Address• Returns offsets of both direct andReturns offsets of both direct and indirect indirect

operands. operands. • LEALEA is is requiredrequired when obtaining the when obtaining the offsetoffset of of

a a local variablelocal variable or stack parameter. or stack parameter.• Example:Example:

CopyString PROCLOCAL temp[20]:BYTE, count:DWORD

mov edi,OFFSET count ; invalid operand

mov esi,OFFSET temp ; invalid operandlea edi,count ; oklea esi,temp ; ok

Page 9: Today's topics

String primitives A string is an array of BYTE In most cases, an extra byte is needed for

the zero-byte terminator MASM has some “string primitives” for

manipulating strings byte-by-byte Most important are:

lodsb ; load string byte stosb ; store string byte cld ; clear direction flag std ; set direction flag

There are many others Explore on your own

Page 10: Today's topics

lodsb and stosb

lodsb Moves byte at [esi] into the AL register Increments esi if direction flag is 0 Decrements esi if direction flag is 1

stosb Moves byte in the AL register to memory at

[edi] Increments edi if direction flag is 0 Decrements edi if direction flag is 1

Page 11: Today's topics

cld and std

cld Sets direction flag to 0 Causes esi and edi to be incremented by lodsb

and stosb Used for moving “forward” through an array

std Sets direction flag to 1 Causes esi and edi to be decremented by lodsb

and stosb Used for moving “backward” through an array

Page 12: Today's topics

Demo ProgramDemo Program

Linked to course website Linked to course website “Resources” page“Resources” page demo6.asmdemo6.asm: Shows capitalizing and : Shows capitalizing and

reversing a stringreversing a string

Page 13: Today's topics

Questions on …

Arrays ? Parameter passing ? System stack ?

Stack frame ? Local variables?

Strings?

Page 14: Today's topics

Procedure (summary) Separate, named section of code.

May have parameters Calling mechanism Return mechanism

During assembly, procedure code is translated once.

During execution, control is transferred to the procedure at each call (activation record, etc.). May be called many times.

All labels, etc. are local to the activation record.

Page 15: Today's topics

Macro

Separate, named section of code May have parameters

Once defined, it can be invoked (called) one or more times. Use name only (don’t use CALL)

During assembly, entire macro code is substituted for each call (expansion) Similar to a constant Invisible to the programmer

Page 16: Today's topics

Defining Macros A macro must be defined before it

can be used. Parameters are optional. Each parameter follows the rules for

identifiers. Syntax:

macroname MACRO [parameter-1, parameter-2,...]

statement-list

ENDM

Page 17: Today's topics

Invoking Macros To invoke a macro, just give the

name and the arguments (if any). Each argument matches a declared

parameter. Each parameter is replaced by its

corresponding argument when the macro is expanded.

When a macro expands, it generates assembly language source code.

Page 18: Today's topics

Example macro definition and call

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

ENDM.datastr1 BYTE "Welcome!",10,13,0str2 BYTE "Please tell me your name ",0.code

. . . mWriteStr str1mWriteStr str2. . .

Sets up registers and uses Irvine's library WriteString

Page 19: Today's topics

Example macro expansion

1 push edx1 mov edx,OFFSET str11 call WriteString1 pop edx

The expanded code shows how the str1 argument replaced the parameter named buffer:

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

ENDM

Page 20: Today's topics

Example macro definition and call

mReadStr MACRO varNamepush ecxpush edxmov edx,OFFSET varNamemov ecx,(SIZEOF varName) – 1 ; Why?call ReadStringpop edxpop ecx

ENDM.datafirstName BYTE 30 DUP(?).code

. . . mReadStr firstName. . .

The mReadStr macro provides a convenient wrapper around ReadString procedure calls.

Page 21: Today's topics

A more complex macro

seq macro a, b ; Print a sequence

moveax,a ; from a to b

movebx,b

test:

cmpeax,ebx ; if a <= b

jg quit ; print a and repeat

call WriteDec ; otherwise quit

inceax

jmptest

quit:

endm

Page 22: Today's topics

What's the problem?

Code is expanded for each call If the macro is called more

than once . . . Duplicate labelsRegister conflicts

Page 23: Today's topics

Duplicate labels You can specify that a label is LOCAL MASM handles the problem by

appending a unique number to the label

SeqSeq macromacro a, ba, bLOCALLOCAL testtest

; Print a sequence; Print a sequencemovmov eax,aeax,a ; from a to b; from a to bmovmov ebx,bebx,b

test:test:cmpcmp eax,ebxeax,ebx ; if a <= b; if a <= bjgjg quitquit. . . . . .

Page 24: Today's topics

Local variables in macros You can specify that a variable is

LOCALseq macroseq macro a, ba, b ; Print a sequence; Print a sequence

LOCALLOCAL test test ; from a to b; from a to bLOCALLOCAL sumsum.data.data ;; data segment;; data segmentsum DWORDsum DWORD ?? ;; define local var;; define local var.code.code ;; code segment;; code segmentmovmov eax,aeax,amovmov ebx,bebx,b

test:test:cmpcmp eax,ebxeax,ebx ; if a <= b; if a <= bjgjg quitquit. . . . . .

Page 25: Today's topics

Parameters

Arguments are substituted exactly as entered, so any valid argument can be used.

Example calls to seq :seq x,y ;memory

seq ecx,edx ;registers

seq 1,20 ;literals

Page 26: Today's topics

Macro vs. Procedure Macros are very convenient, easy to

understand Macros actually execute faster than

procedures No return address, stack manipulation, etc.

Macros are invoked by name Parameters are “in-line” Macro does not have a ret statement

(Why?) Why would you ever use a procedure

instead of a macro? If the macro is called many times, the

assembler produces “fat code” Invisible to the programmer Each macro call expands the program code by

the length of the macro code

Page 27: Today's topics

Macro vs. Procedure Use a macro for short code that is called

“a few” times. Use a procedure for more complex tasks

or code that is called “many” times The terms “few” and “many” are relative to

the size of the whole program

Is it OK to invoke a macro inside of a loop that executes 1000 times ?

Is it OK to invoke a procedure inside of a loop that executes 1000 times ?

Page 28: Today's topics

Demo ProgramDemo Program

Linked to course website Linked to course website “Resources” page“Resources” page demo7.asmdemo7.asm: shows macros, macro : shows macros, macro

calls, and macro parameterscalls, and macro parameters

Page 29: Today's topics

Questions?Questions?

If learning assembly language If learning assembly language does does nothing else nothing else for you, it for you, it makes you appreciate high-level makes you appreciate high-level languages.languages.


Recommended