Thursday, January 21 - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Thursday, January 21

Description:

Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th – PowerPoint PPT presentation

Number of Views:127
Avg rating:3.0/5.0
Slides: 39
Provided by: Paul7376
Category:

less

Transcript and Presenter's Notes

Title: Thursday, January 21


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

2
Todays topics
  • Assembly language program development
  • Introduction to MASM

3
Assembler, Linker, IDE
  • http//kipirvine.com/asm/gettingStarted
  • Install Visual C 2008 Express Edition (if you
    dont 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.

4
Additional resources
  • Course website Resources
  • MASM Guide
  • MASM instruction set
  • Template for all programs
  • Demo programs
  • Etc.

5
Program development
  • Design
  • Implement
  • Test / Debug
  • Use and maintain

6
Development tools
  • Editor
  • Assembler
  • Libraries
  • Linker
  • Loader
  • Operating system

7
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
8
MASM instruction types
  • Move data
  • Arithmetic
  • Compare two values
  • Conditional/unconditional branch
  • Call procedure
  • Loop control
  • I/O

9
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

10
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

11
MASM data types
Type 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
12
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.

13
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

14
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
15
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

16
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

17
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)

18
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

19
main procedure
  • in the .code segment
  • General form is
  • main PROC
  • (program instructions)
  • main ENDP
  • (other procedures)
  • END main

20
Identifiers
  • 1 to 247 characters (no spaces)
  • NOT case sensitive!
  • Start with letter, _ , _at_, or
  • For now, dont use _ , _at_ , or
  • Remaining characters are letters, digits, or _
  • Identify variables, constants, procedures, and
    labels
  • Cannot be a reserved word

21
Literals
  • Actual values, named constants
  • Assign contents of registers, memory
  • Initialize variables in the .data segment
  • Integer
  • Floating point
  • Character
  • String

22
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

23
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
  • Its,0 Title MASM,0
  • Strings must be null-terminated
  • Always end with zero-byte

24
Directives
  • Tell the assembler how to interpret the code
  • Mark beginning of program segments
  • .data .code
  • Mark special labels
  • main proc

25
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

26
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?)

27
Instructions with implied operands
mul, imul implied operand must be in
eax mul op2 result is in EDXEAX Example mov
eax,10 mov ebx,0Ch mul ebx result is in
eax (120), with possible
overflow in edx
28
Instructions with implied operands
div, idiv implied operand is in EDXEAX so set
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)
29
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.

30
Library Procedures - Overview p1
  • See IrvineLibHelp.exe at http//classes.engr.orego
    nstate.edu/eecs/winter2010/cs271/resources/Links.h
    tm/
  • 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

31
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

32
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

33
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
34
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
35
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

36
Program Design
  • Decide what the program should do
  • Define algorithm(s)
  • Decide what the output should show
  • Determine what variables/constants are required

37
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

38
Questions?
Program 1 due Friday, July 9th, before
midnight Quiz 2 Thursday, July 15th Read
Irvine Chapter 4
Write a Comment
User Comments (0)
About PowerShow.com