380 likes | 582 Views
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
E N D
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 • Install Visual C++ 2008 Express Edition (if you don’t already have a version of Visual C++) • Install the Microsoft Assembler • Install the libraries • We will use Irvine's library (for now) to handle the really awful stuff. • input / output, screen control, timing, etc.
Additional resources • Course website “Resources” • MASM Guide • MASM instruction set • Template for all programs • Demo programs • Etc.
Program development • Design • Implement • Test / Debug • Use and maintain
Development tools • Editor • Assembler • Libraries • Linker • Loader • Operating system
Text source code (.asm file) Program design and algorithms Text editor Assembler Binary machine code (.obj file) Binary Executable code (.exe file) Linker Library files (.inc, .lib) Loader Instruction Execution Cycle (Operating System) Binary program in memory execution begins
MASM instruction types • Move data • Arithmetic • Compare two values • Conditional/unconditional branch • Call procedure • Loop control • I/O
Instruction formats • Variable length • Opcode • Fixed length • Operand specification • different “addressing modes” for different opcodes • different number of operands for different opcodes • opcode • opcode destination • opcode destination, source
Addressing modes • Immediate Set register to a constant • Direct Set register to address of global • Register Use register as operand • Register indirect Access memory through address in a register • Indexed “array” element, using offset in register • Base-indexed Start address in one register; offset in another, add and access memory • Stack Memory area specified and maintained as a stack; stack pointer in register • Offset (branch) “goto” address; may be computed
Memory locations • May be named • Name can refer to a variable name or a program label • Interpretation of contents depends on program instructions • Numeric data • Integer, floating point • Non-numeric data • Character, string • Instruction • Address • etc.
General form of a MASM statement • Comments start with ; • Segments start with . • Each instruction line has 4 fields: • Label • Opcode • Operands • Comment • Depending on the opcode, one or more operands may be required • Otherwise, any field may be empty • If empty opcode field, operand field must be empty
TITLE Program Template (template.asm) ; Author: ; Course/project ID Date: ; Description: ; (include any libraries here) ; (insert symbol definitions here) .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit ; exit to operating system main ENDP ; (insert additional procedures here) END 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 definition • in the .data segment • General form is label data_type initializer(s) ;comment • label is the "variable name" • data_type is one of (see previous slide) • at least one initializer is required • may be ? (value to be assigned later) • Examples: size DWORD 100 ;class size temperature SWORD -10 ;current Celsius response BYTE 'Y' ;positive answer gpa REAL4 ? ;my GPA myName BYTE ”Wile E. Coyote”,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 labels main proc
Instructions • For now, know how to use mov add sub mul, imul div, idiv inc dec loop • Some instructions use implied operands • See Irvine (Appendix B) or on-line Instructions
Easy Instructions mov op1, op2 ;op2 is copied to op1 add op1, op2 ;op2 is added to op1 sub op1, op2 ;op2 is subtracted from op1 inc op ; add 1 to op dec 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, imul implied operand must be in eax mul op2 ; result is in EDX:EAX Example: mov eax,10 mov ebx,0Ch mul ebx ; result is in eax (120), ; with possible ; overflow in edx
Instructions with implied operands div,idiv implied operand is in EDX:EAX soset edx to 0 before division div op2 ; quotient is in EAX ; remainder is in EDX Example: mov eax,100 mov edx,0 mov ebx,9 div ebx ; quotient is in eax (11) ; remainder is in edx (1)
Instructions with implied operands loop implied operand is ecx soset ecx to the loop count, andput a label at the beginning of the loop mov ecx,10 repeat: ; 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 • 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: INCLUDE Irvine32.inc ... mov eax,1234 ; input argument call WriteDec ; show number call Crlf ; end of line
Calling a Library Procedure • 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. INCLUDE Irvine32.inc ... mov saveA,eax ;save the eax register mov eax,-123 ;value to display call WriteInt ;show number call Crlf ;end of line mov eax,saveA ;restore eax
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,size sub eax,absent mov present,eax • OK … mov eax,size ;start with class size sub eax,absent ;subtract absentees mov present,eax ;number present • Terrible … mov eax,size ;move size into eax sub eax,absent ;subtract absent from eax mov present,eax ;move eax to present
Program Design • Decide what the program should do • Define algorithm(s) • Decide what the output should show • Determine what variables/constants are required
Implementing a MASM program • Open project • Start with template • Save as program name (.asm) • Fill in header information • Define constants • Test*/fix (syntax check, nothing happens) • Declare variables (.data section) • Test*/fix (syntax check, nothing happens) • Enter the output code • Test*/fix (no calculations, usually everything shows 0) • Enter the input code • Test*/fix (no calculations, echo input) • Enter the calculation code • Test*/fix (logic check, verify) * First try Debug, Start Without Debugging
Questions? Program #1 due Friday, July 9th, before midnight Quiz #2 Thursday, July 15th Read Irvine Chapter 4