Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th."— Presentation transcript:

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

2 Today’s topics Assembly language program development Introduction to MASM

3 Assembler, Linker, IDE Install Visual C Express Edition (if you don’t already have a version of Visual C++) Install Visual C Express Edition (if you don’t already have a version of Visual C++) Install the Microsoft Assembler Install the Microsoft Assembler Install the libraries Install the libraries We will use Irvine's library (for now) to handle the really awful stuff. We will use Irvine's library (for now) to handle the really awful stuff. input / output, screen control, timing, etc. input / output, screen control, timing, etc.

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

5 Program development Design Implement Test / Debug Use and maintain

6 Development tools Editor Assembler Libraries Linker Loader Operating system

7 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

8 MASM instruction types Move data Move data Arithmetic Arithmetic Compare two values Compare two values Conditional/unconditional branch Conditional/unconditional branch Call procedure Call procedure Loop control Loop control I/O I/O

9 Instruction formats Variable length Variable length Opcode Opcode Fixed length Fixed length Operand specification Operand specification different “addressing modes” for different opcodes different “addressing modes” for different opcodes different number of operands for different opcodes different number of operands for different opcodes opcode opcode opcodedestination opcodedestination opcodedestination, source opcodedestination, source

10 Addressing modes Immediate Set register to a constant Immediate Set register to a constant Direct Set register to address of global Direct Set register to address of global Register Use register as operand Register Use register as operand Register indirect Access memory through address in a register Register indirect Access memory through address in a register Indexed “array” element, using offset in register Indexed “array” element, using offset in register Base-indexed Start address in one register; offset in another, add and access memory 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 Stack Memory area specified and maintained as a stack; stack pointer in register Offset (branch) “ goto” address; may be computed Offset (branch) “ goto” address; may be computed

11 MASM data types TypeUsed 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 [ … 32767] DWORD 4-byte unsigned integers [0 … ], address SDWORD 4-byte signed integers [ … ] 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 May be named Name can refer to a variable name or a program label Name can refer to a variable name or a program label Interpretation of contents depends on program instructions Interpretation of contents depends on program instructions Numeric data Numeric data Integer, floating point Integer, floating point Non-numeric data Non-numeric data Character, string Character, string Instruction Instruction Address Address etc. etc.

13 General form of a MASM statement Comments start with ; Comments start with ; Segments start with. Segments start with. Each instruction line has 4 fields: Each instruction line has 4 fields: Label Label Opcode Opcode Operands Operands Comment Comment Depending on the opcode, one or more operands may be required Depending on the opcode, one or more operands may be required Otherwise, any field may be empty Otherwise, any field may be empty If empty opcode field, operand field must be empty If empty opcode field, operand field must be empty

14 TITLE Program Template (template.asm) ; Author: ; Course/project IDDate: ; 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 in the.data segment General form is General form is labeldata_type initializer(s);comment label is the "variable name" label is the "variable name" data_type is one of (see previous slide) data_type is one of (see previous slide) at least one initializer is required at least one initializer is required may be ?(value to be assigned later) may be ?(value to be assigned later) Examples: Examples: sizeDWORD100;class size temperatureSWORD-10;current Celsius responseBYTE'Y';positive answer gpaREAL4?;my GPA myNameBYTE ” Wile E. Coyote ”,0

19 main procedure in the.code segment General form is mainPROC ; ( program instructions ) mainENDP ; ( other procedures ) ENDmain

20 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

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., ) Exponent notation(e.g.,-3.5E26.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 ” It ’ s ”,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 mainproc

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 movop1, op2;op2 is copied to op1 addop1, op2;op2 is added to op1 subop1, op2;op2 is subtracted from op1 incop; add 1 to op decop; 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, imulimplied operand must be in eax mulop2; result is in EDX:EAX Example: moveax,10 movebx,0Ch mulebx; result is in eax (120), ; with possible ; overflow in edx

28 Instructions with implied operands div, idivimplied operand is in EDX:EAX so set edx to 0 before division divop2; quotient is in EAX ; remainder is in EDX Example: moveax,100 movedx,0 movebx,9 divebx; quotient is in eax (11) ; remainder is in edx (1)

29 Instructions with implied operands loopimplied operand is ecx so set ecx to the loop count, and put a label at the beginning of the loop movecx,10 repeat: ; loop body ;… looprepeat 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 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 INCLUDE Irvine32.inc... mov eax,1234; input argument call WriteDec; show number call 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:

34 Calling a Library Procedure 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 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.

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. moveax,size subeax,absent movpresent,eax OK … moveax,size;start with class size subeax,absent;subtract absentees movpresent,eax;number present Terrible … moveax,size;move size into eax subeax,absent;subtract absent from eax movpresent,eax;move eax to present

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

37 Implementing a MASM program Open project Open project Start with template Start with template Save as program name (.asm) Save as program name (.asm) Fill in header information Fill in header information Define constants Define 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 output code Enter the output code Test*/fix (no calculations, usually everything shows 0) Test*/fix (no calculations, usually everything shows 0) Enter the input code Enter the input code Test*/fix (no calculations, echo input) Test*/fix (no calculations, echo input) Enter the calculation code Enter the calculation code Test*/fix (logic check, verify) 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


Download ppt "Thursday, January 21 Program #1 is posted Due Friday, June 9th Quiz #2 Thursday, July 15th."

Similar presentations


Ads by Google