Upload
cutter
View
64
Download
0
Tags:
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
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, 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.
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.
Program development
Design Implement Test / Debug Use and maintain
Development tools
Editor Assembler Libraries Linker Loader Operating system
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
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
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
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
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
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.
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
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
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
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
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)
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
main procedure in the .code segment General form is
main PROC
; (program instructions)
main ENDP
; (other procedures)
END main
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
Literals Actual values, named constants
Assign contents of registers, memory Initialize variables in the .data segment
Integer Floating point Character String
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
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
Directives
Tell the assembler how to interpret the code
Mark beginning of program segments.data .code
Mark special labelsmain proc
Instructions For now, know how to use
movaddsubmul, imuldiv, idivincdecloop
Some instructions use implied operands See Irvine (Appendix B) or on-line
Instructions
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?)
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
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)
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.
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
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
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
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:
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.
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
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
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
Questions?Program #1
due Friday, July 9th, before midnight
Quiz #2 Thursday, July 15th
Read Irvine Chapter 4Read Irvine Chapter 4