38
Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th

Thursday, January 21

  • Upload
    cutter

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

Thursday, January 21. Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th. Today’s topics. Assembly language program development Introduction to MASM. Assembler, Linker, IDE. http://kipirvine.com/asm/gettingStarted - PowerPoint PPT Presentation

Citation preview

Page 1: Thursday, January 21

Thursday, January 21

Program #1 is posted Due Friday, June 9th

Quiz #2 Thursday, July 15th

Page 2: Thursday, January 21

Today’s topics

Assembly language program development

Introduction to MASM

Page 3: Thursday, January 21

Assembler, Linker, IDEAssembler, Linker, IDE

http://kipirvine.com/asm/http://kipirvine.com/asm/gettingStartedgettingStarted

Install Install Visual C++ 2008 Express Visual C++ 2008 Express EditionEdition (if you don’t already have a (if you don’t already have a version of version of Visual C++Visual C++))

Install the Install the Microsoft AssemblerMicrosoft Assembler Install the libraries Install the libraries We will use Irvine's library (for now) We will use Irvine's library (for now)

to handle the really awful stuff.to handle the really awful stuff. input / output, screen control, timing, input / output, screen control, timing,

etc.etc.

Page 4: Thursday, January 21

Additional resourcesAdditional resources

Course website “Resources”Course website “Resources” MASM GuideMASM Guide MASM instruction setMASM instruction set Template for all programsTemplate for all programs Demo programsDemo programs Etc.Etc.

Page 5: Thursday, January 21

Program development

Design Implement Test / Debug Use and maintain

Page 6: Thursday, January 21

Development tools

Editor Assembler Libraries Linker Loader Operating system

Page 7: Thursday, January 21

Program design and algorithms Text

editor

Text source code (.asm file)

Assembler

Library files (.inc, .lib)

Linker

Binary Executable code (.exe file)

Binary program in memory

Binary machine code(.obj file)

Instruction Execution Cycle (Operating System) execution

begins

Loader

Page 8: Thursday, January 21

MASM instruction typesMASM instruction types

Move dataMove data ArithmeticArithmetic Compare two valuesCompare two values Conditional/unconditional branchConditional/unconditional branch Call procedureCall procedure Loop controlLoop control I/O I/O

Page 9: Thursday, January 21

Instruction formatsInstruction formats

Variable lengthVariable length OpcodeOpcode

Fixed lengthFixed length Operand specificationOperand specification

different “addressing modes” for different different “addressing modes” for different opcodesopcodes

different number of operands for different different number of operands for different opcodesopcodes

opcodeopcode opcodeopcode destinationdestination opcodeopcode destination, sourcedestination, source

Page 10: Thursday, January 21

Addressing modesAddressing modes ImmediateImmediate Set register to a constantSet register to a constant

DirectDirect Set register to address of globalSet register to address of global

RegisterRegister Use register as operandUse register as operand

Register indirectRegister indirect Access memory through address Access memory through address in a in a registerregister

IndexedIndexed “array” element, using offset in register“array” element, using offset in register

Base-indexedBase-indexed Start address in one register; offset in Start address in one register; offset in another, add and access another, add and access

memorymemory

StackStack Memory area specified and Memory area specified and maintained maintained as a stack; stack as a stack; stack pointer in registerpointer in register

Offset (branch) Offset (branch) ““goto” address; may be goto” address; may be computedcomputed

Page 11: Thursday, January 21

MASM data typesMASM data typesType Used for:

BYTE Strings, 1-byte unsigned integers [0 … 255]

SBYTE 1-byte signed integers [-128 … 127]

WORD 2-byte unsigned integers [0 … 65535], address

SWORD 2-byte signed integers [-32768 … 32767]

DWORD 4-byte unsigned integers [0 … 4294967295], address

SDWORD 4-byte signed integers [-2147483648 … 2147483647]

FWORD 6-byte integer

QWORD 8-byte integer

TBYTE 10-byte integer

REAL4 4-byte floating-point

REAL8 8-byte floating-point

REAL10 10-byte floating-point

Page 12: Thursday, January 21

Memory locationsMemory locations May be namedMay be named

Name can refer to a variable name or a Name can refer to a variable name or a program labelprogram label

Interpretation of contents Interpretation of contents depends depends on program instructionson program instructions Numeric dataNumeric data

Integer, floating pointInteger, floating point Non-numeric dataNon-numeric data

Character, stringCharacter, string InstructionInstruction AddressAddress etc.etc.

Page 13: Thursday, January 21

General form of a MASM General form of a MASM statementstatement

Comments start with Comments start with ;; Segments start with Segments start with .. Each instruction line has 4 fields:Each instruction line has 4 fields:

LabelLabel OpcodeOpcode OperandsOperands CommentComment

Depending on the opcode, one or more Depending on the opcode, one or more operands may be requiredoperands may be required Otherwise, any field may be emptyOtherwise, any field may be empty If empty opcode field, operand field must be If empty opcode field, operand field must be

emptyempty

Page 14: Thursday, January 21

TITLE Program Template (template.asm)TITLE Program Template (template.asm)

; Author:; Author:; Course/project ID; Course/project ID Date:Date:; Description:; Description:

; (include any libraries here); (include any libraries here)

; (insert symbol definitions here); (insert symbol definitions here)

.data.data; (insert variables here); (insert variables here)

.code.codemain PROCmain PROC

; (insert executable instructions here); (insert executable instructions here)

exitexit ; exit to operating system; exit to operating systemmain ENDPmain ENDP

; (insert additional procedures here); (insert additional procedures here)

END mainEND main

Page 15: Thursday, January 21

Getting started

We will use Irvine's library (for now) to handle the really awful stuff. input / output screen control timing etc.

Check the Resources page, MASM Example program development walk-

through

Page 16: Thursday, January 21

MASM program

TITLE directive you can put anything you want … but the grader wants to see a

meaningful title and the name of the source code file

; identification block technically optional (as are all

comments) … but the grader wants to see

information (see template.asm) INCLUDE directive

copies a file of definitions and procedures into the source code

use Irvine32.inc for now

Page 17: Thursday, January 21

MASM program

Global constants may be defined .data directive

marks beginning of data segment variable declarations go here

.code directive marks end of data segment and

beginning of code segment main procedure defined here (required) other procedures defined here (optional)

Page 18: Thursday, January 21

Data definitionData definition in the in the .data.data segment segment General form is General form is

labellabel data_typedata_type initializer(s) initializer(s) ;comment;comment labellabel is the "variable name" is the "variable name" data_typedata_type is one of is one of (see previous slide)(see previous slide) at least one at least one initializerinitializer is required is required

may be may be ?? (value to be assigned later)(value to be assigned later) Examples:Examples:

sizesize DWORDDWORD 100100 ;class size;class sizetemperaturetemperature SWORDSWORD -10-10 ;current Celsius;current Celsiusresponseresponse BYTEBYTE 'Y''Y' ;positive answer;positive answergpagpa REAL4REAL4 ?? ;my GPA;my GPAmyNamemyName BYTEBYTE ””Wile E. CoyoteWile E. Coyote””,0,0

Page 19: Thursday, January 21

main procedure in the .code segment General form is

main PROC

; (program instructions)

main ENDP

; (other procedures)

END main

Page 20: Thursday, January 21

Identifiers 1 to 247 characters (no spaces) NOT case sensitive! Start with letter, _ , @, or $

For now, don’t use _ , @ , or $ Remaining characters are letters,

digits, or _ Identify variables, constants,

procedures, and labels Cannot be a reserved word

Page 21: Thursday, January 21

Literals Actual values, named constants

Assign contents of registers, memory Initialize variables in the .data segment

Integer Floating point Character String

Page 22: Thursday, January 21

Literals Integer

Optional radix: b, q/o, d, h Digits must be consistent with radix Hex values that start with a letter must have leading

0 Default (no radix) is decimal

Floating-point (real) Optional sign Standard notation (e.g., -3.5 +5.

7.2345) Exponent notation (e.g., -3.5E2

6.15E-3) Must have decimal point

Page 23: Thursday, January 21

Literals Character

Single character in quotes ’a’ ”*” Single quotes recommended

String Characters in quotes

’always’,0 ”123 * 654”,0 Double quotes recommended Embedded quotes must be different

”It’s”,0 ’Title: ”MASM”’,0 Strings must be null-terminated

Always end with zero-byte

Page 24: Thursday, January 21

Directives

Tell the assembler how to interpret the code

Mark beginning of program segments.data .code

Mark special labelsmain proc

Page 25: Thursday, January 21

Instructions For now, know how to use

movaddsubmul, imuldiv, idivincdecloop

Some instructions use implied operands See Irvine (Appendix B) or on-line

Instructions

Page 26: Thursday, January 21

Easy Instructionsmov op1, op2 ;op2 is copied to op1add op1, op2 ;op2 is added to op1sub op1, op2 ;op2 is subtracted from op1inc op ; add 1 to opdec op ; subtract 1 from op

For 2-operand instructions the first operand is the destination and the second operand is the source

2-operand instructions require at least one of the operands to be a register (or op2 must be literal). Note: op1 can not be a literal ! (Why?)

Page 27: Thursday, January 21

Instructions with implied operands

mul, imulimplied operand must be in eaxmul op2 ; result is in EDX:EAX

Example:mov eax,10mov ebx,0Chmul ebx; result is in eax (120),

; with possible ; overflow in edx

Page 28: Thursday, January 21

Instructions with implied operands

div, idiv implied operand is in EDX:EAXso set edx to 0 before divisiondiv op2 ; quotient is in EAX

; remainder is in EDXExample:mov eax,100mov edx,0mov ebx,9div ebx ; quotient is in eax (11)

; remainder is in edx (1)

Page 29: Thursday, January 21

Instructions with implied operands

loop implied operand is ecxso set ecx to the loop count, and put a label at the beginning of the loop

mov ecx,10repeat:

; loop body; …loop repeat

ecx is automatically decremented by 1 and tested each time the loop statement is executed. When ecx becomes 0, the loop terminates.

Page 30: Thursday, January 21

Library Procedures - Overview p1

See IrvineLibHelp.exe at http://classes.engr.oregonstate.edu/eecs/winter2010/cs271/resources/Links.htm/

Clrscr : Clear the screen Preconditions: none Postconditions: screen cleared and cursor is at

upper left corner Crlf : New line

Preconditions: none Postconditions: cursor is at beginning of next

new line

Page 31: Thursday, January 21

Library Procedures - Overview p2

ReadInt : Reads a 32-bit signed decimal integer from keyboard, terminated by the Enter key. Preconditions: none Postconditions: value entered is in EAX

ReadString : Reads a string from keyboard, terminated by the Enter key. Preconditions: OFFSET of memory destination in

EDX Size of memory destination in ECX Postconditions: String entered is in memory Length of string entered is in EAX

Page 32: Thursday, January 21

Library Procedures - Overview p3 WriteDec : Writes an unsigned 32-bit

integer to the screen in decimal format. Preconditions: value in EAX Postconditions: value displayed

WriteInt - Writes a signed 32-bit integer to the screen in decimal format. Preconditions: value in EAX Postconditions: value displayed

WriteString - Writes a null-terminated string to the screen. Preconditions: OFFSET of memory location in

EDX Postconditions: string displayed

Page 33: Thursday, January 21

Calling a Library Procedure

INCLUDE Irvine32.inc...

mov eax,1234 ; input argumentcall WriteDec ; show numbercall Crlf ; end of line

• Call a library procedure using the CALL instruction.

• Some procedures require input arguments.

• The INCLUDE directive copies the procedure prototypes (declarations) into the program source code.

• Example: display "1234" on the console:

Page 34: Thursday, January 21

Calling a Library Procedure

INCLUDE Irvine32.inc...

mov saveA,eax ;save the eax registermov eax,-123 ;value to displaycall WriteInt ;show numbercall Crlf ;end of linemov eax,saveA ;restore eax

• Sometimes certain registers are used to pass parameters

• Sometimes values of registers must be saved (in memory) before calling a procedure, and restored to the original values when control returns.

Page 35: Thursday, January 21

In-line Comments Start with ; May be on separate line or at end of a line Use comments to clarify lines or sections Preferred …

; Calculate the number of students in class today.mov eax,sizesub eax,absentmov present,eax

OK …mov eax,size ;start with class

sizesub eax,absent ;subtract absenteesmov present,eax ;number present

Terrible …mov eax,size ;move size into eaxsub eax,absent ;subtract absent

from eaxmov present,eax ;move eax to

present

Page 36: Thursday, January 21

Program DesignProgram Design

Decide what the program should doDecide what the program should do Define algorithm(s)Define algorithm(s) Decide what the output should showDecide what the output should show Determine what variables/constants Determine what variables/constants

are requiredare required

Page 37: Thursday, January 21

Implementing a MASM Implementing a MASM programprogram

Open projectOpen project Start with templateStart with template

Save as program name (.asm)Save as program name (.asm) Fill in header informationFill in header information Define constantsDefine constants

Test*/fix (syntax check, nothing happens) Test*/fix (syntax check, nothing happens) Declare variables (.data section)Declare variables (.data section)

Test*/fix (syntax check, nothing happens)Test*/fix (syntax check, nothing happens) Enter the Enter the outputoutput code code

Test*/fix (no calculations, usually everything shows 0)Test*/fix (no calculations, usually everything shows 0) Enter the Enter the inputinput code code

Test*/fix (no calculations, echo input)Test*/fix (no calculations, echo input) Enter the Enter the calculationcalculation code code

Test*/fix (logic check, verify)Test*/fix (logic check, verify)

* First try * First try DebugDebug, , Start Without Debugging Start Without Debugging

Page 38: Thursday, January 21

Questions?Program #1

due Friday, July 9th, before midnight

Quiz #2 Thursday, July 15th

Read Irvine Chapter 4Read Irvine Chapter 4