Lecture #6

advertisement
Thursday, January 21
 Program
 Due
#1 is posted
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
Program
design and
algorithms
Binary
Executable
code
(.exe file)
Text editor
Text
source
code
(.asm file)
Binary
machine
code
(.obj file)
Library
files
(.inc, .lib)
Linker
Loader
Binary
program in
memory
Assembler
Instruction
Execution
Cycle
(Operating
System)
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
Direct
Register
Register indirect
Set register to a constant
“array” element, using offset in register

Indexed
Base-indexed

Stack
Memory area specified and maintained
as a stack; stack pointer in register

Offset (branch)
“goto” address; may be computed





Set register to address of global
Use register as operand
Access memory through address in a
register
Start address in one register; offset in
another, add and access memory
MASM data types
Type
BYTE
Used for:
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
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


Non-numeric data




Integer, floating point
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
; Description:
Date:
; (include any libraries here)
; (insert symbol definitions here)
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit
main ENDP
; exit to operating system
; (insert additional procedures here)
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



initializer(s)
;comment
label is the "variable name"
data_type is one of (see previous slide)
at least one initializer is required


data_type
may be ?
(value to be assigned later)
Examples:
size
DWORD
temperature
response BYTE
gpa
REAL4
myName
BYTE
100
;class size
SWORD -10
;current Celsius
'Y'
;positive answer
?
;my GPA
”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

Integer

Optional radix: b, q/o, d, h




Literals
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.
Exponent notation (e.g., -3.5E2
Must have decimal point
7.2345)
6.15E-3)

Character

Literals
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
add
sub
inc
dec


op1, op2
op1, op2
op1, op2
op
op
;op2 is copied to op1
;op2 is added to op1
;op2 is subtracted from op1
; add 1 to 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
mov
mul
eax,10
ebx,0Ch
ebx
; result is in eax (120),
;
with possible
;
overflow in edx
Instructions with implied operands
div, idiv implied operand is in EDX:EAX
so set edx to 0 before division
div op2
; quotient is in EAX
; remainder is in EDX
Example:
mov
mov
mov
div
eax,100
edx,0
ebx,9
ebx
; quotient is in eax (11)
; remainder is in edx (1)
Instructions with implied operands
loop
implied operand is ecx
so set ecx to the loop count, and put 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.



WriteInt - Writes a signed 32-bit integer to the
screen in decimal format.



Preconditions: value in EAX
Postconditions: value displayed
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
sub
mov

eax,size
eax,absent
present,eax
;start with class size
;subtract absentees
;number present
eax,size
eax,absent
present,eax
;move size into eax
;subtract absent from eax
;move eax to present
Terrible …
mov
sub
mov
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



Fill in header information
Define constants


Test*/fix (no calculations, usually everything shows 0)
Enter the input code


Test*/fix (syntax check, nothing happens)
Enter the output code


Test*/fix (syntax check, nothing happens)
Declare variables (.data section)


Save as program name (.asm)
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
Download