wres 1201 – lab1
TRANSCRIPT
-
8/10/2019 Wres 1201 Lab1
1/27
WRES 1201 LabThis week:
1. Project requirements
2. Revision
3. Sample code
4. Lab assignment
-
8/10/2019 Wres 1201 Lab1
2/27
Assignment
Contributes 20% of your total coursework marks.
32 bit or 16 bit.
Number of assignment: 4
Each assignment contributes 5% marks.
Each assignment must be demo in the lab
session.
Report and the written code. 2 students per group for each assignment.
1stassignment: in the end of this lab session.
-
8/10/2019 Wres 1201 Lab1
3/27
Revision
Basic Elements of Assembly Language.
Adding and Subtracting Integers example
of code
-
8/10/2019 Wres 1201 Lab1
4/27
Integer Constants
Optional leading +or sign
binary, decimal, hexadecimal, or octal digits
Common radix characters:
h hexadecimal
d decimal b binary
r encoded real
q/o - octal
Examples: 30d, 6Ah, 42, 1101b, 33q, 22o
Hexadecimalbeginning with letter must have a leading zero toprevent the assembler interpreting it as an identifier : 0A5h
If no radix:decimal
-
8/10/2019 Wres 1201 Lab1
5/27
Integer Expressions
Operators and precedence levels:
Examples:
-
8/10/2019 Wres 1201 Lab1
6/27
Character and String Constants
Enclose characterin single or double quotes
'A', "x"
ASCII character = 1 byte
Enclose stringsin single or double quotes
"ABC"
'xyz'
Each character occupies a single byte
Embedded quotes:
'Say "Goodnight," Gracie'
-
8/10/2019 Wres 1201 Lab1
7/27
Reserved Words
Reserved words(Appendix D) cannot be used as identifiers
Instruction mnemonics (MOVE, ADD, MUL)
Directives (INCLUDE)
type attributes (BYTE, WORD)
Operators (+, -) predefined symbols (@data)
Directives
command understood by the assembler not part of Intel instruction set
case insensitive
-
8/10/2019 Wres 1201 Lab1
8/27
-
8/10/2019 Wres 1201 Lab1
9/27
Directives
Commandsthat are recognized / understoodand acted upon
by the assembler
Not part of the Intel instruction set
Used to declare code, data areas, select memory model, declare
procedures, etc. Not case sensitive (.data, .DATA, .Data - same)
Different assemblers have different directives
NASM != MASM, for example
Examples : .DATAidentify area of a program that contains variables
.CODEidentify area of a program that contains instructions
PROCidentify beginning of a procedure. name PROC
-
8/10/2019 Wres 1201 Lab1
10/27
Instructions
Assembled into machine codeby assembler
Executed at runtime by the CPU after the program has been
loaded into memory and started.
Member of the Intel IA-32 instruction set
Parts :
A) Label (optional)
B) Mnemonic (required)
C) Operand (required)
D) Comment (optional)
Label : Mnemonics Operand(s) ; Comment
-
8/10/2019 Wres 1201 Lab1
11/27
A) Labels
Act as place markermarks the address (offset) of codeand data
Follow identifierrules(refer slide pg 8)
Datalabel
must be unique
example: myArray
mov ax, myVariable
Codelabel
target of jump and loop instructions
example: L1:target:
mov ax,bx
jmp target
-
8/10/2019 Wres 1201 Lab1
12/27
B) Mnemonics and Operands
Instruction Mnemonics is a short word that identifies the operation
carried out by an instruction. (useful name)
Examples :
MOV move (assign one value to another)
ADD add two values
SUB subtract one value from another
MUL multiply two values
INC increment
DEC decrement
JMP jump to a new location
CALL call a procedure
-
8/10/2019 Wres 1201 Lab1
13/27
C) Operands
An assembly language can have between zero to three
operands
Types of operand :
constant (immediate value)
96, 2005h, 101011010b
constant expression
2+4
register
EAX, EBX, AX, AH
memory(data label)
count
-
8/10/2019 Wres 1201 Lab1
14/27
.. C) Operands
Examples assembly language instructions with various
number of operands :
No operand
stc ; set carry flag
one operand
inc ax ; add 1 to AX
two operand
mov count, bx ; move BX to count
-
8/10/2019 Wres 1201 Lab1
15/27
D) Comments
Comments are good.
explain the program's purpose
when it was written, and by whom
revision information
tricky coding techniques
application-specific explanations
Single-line comments
begin with semicolon (;)
Ex : ; add BX to AX
Multi-line comments
begin with COMMENT directive and a programmer-chosen character
end with the same programmer-chosen character Ex :
COMMENT !
This is a comment
This line is also a comment
Comment lagi dan lagi dan lagi.
Lepas ni habis ekk
!
-
8/10/2019 Wres 1201 Lab1
16/27
Instruction Format Examples
No operands
stc ; set Carry flag
One operand
inc eax ; register inc myByte ; memory
Two operands
add ebx,ecx ; register, register
sub myByte,25 ; memory, constant
add eax,36 * 25 ; register, constant expression
-
8/10/2019 Wres 1201 Lab1
17/27
TITLE Kira Baki Biasiswa (pokai.asm)
;Program Description:
; Author:
; Date Created:
; Last Modification Date:
INCLUDE Irvine32.inc
.data
(insert variables here)
val1 dword 10000h
.code
main PROC
(insert executable instructions here)
mov ax,@data ; initialize DS
exit ; exit to operating system
main ENDP
(insert additional procedures here)
END main
-
8/10/2019 Wres 1201 Lab1
18/27
Example: Adding and Subtracting
Integers
TITLEAdd and Subtract (AddSub.asm)
; This program adds and subtracts 32-bit integers.
INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h ; EAX = 10000h
add eax,40000h ; EAX = 50000h
sub eax,20000h ; EAX = 30000h
call DumpRegs ; display registers
exit
main ENDP
END main
-
8/10/2019 Wres 1201 Lab1
19/27
19
mov eax,10000h
0010 0000EAX 10000 h
add eax,40000h
0050 0000EAX10000 h + 40000 h
= 50000 h
sub eax,10000h
0030 0000EAX50000 h 20000 h
= 30000 h
-
8/10/2019 Wres 1201 Lab1
20/27
Example Output
Program output, showing registers and flags:
EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF
ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4
EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0
-
8/10/2019 Wres 1201 Lab1
21/27
Suggested Coding Standards (1 of 2)
Some approaches to capitalization
capitalize nothing
capitalize everything
capitalize all reserved words, including instruction mnemonics
(MOV)and register names (AX,EAX) capitalize only directives(.data)and operators(+,-)
Other suggestions
descriptive identifier names spaces surrounding arithmetic operators
blank lines between procedures
-
8/10/2019 Wres 1201 Lab1
22/27
Suggested Coding Standards (2 of 2)
Indentation and spacing
code anddata labelsno indentation
executable instructionsindent 4-5 spaces
comments: begin at column 40-45, aligned
vertically
1-3 spaces between instruction and its
operands ex: mov ax,bx
1-2 blank lines between procedures
-
8/10/2019 Wres 1201 Lab1
23/27
-
8/10/2019 Wres 1201 Lab1
24/27
Program Template
TITLE Program Template (Template.asm)
; Program Description:
; Author:
; Creation Date:
; Revisions:
; Date: Modified by:
INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC ; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
Instruction: please
customize as needed
-
8/10/2019 Wres 1201 Lab1
25/27
Assignment 1
Write a program MASM assembly langguage to have the input
and output as follows:
Input:
-
8/10/2019 Wres 1201 Lab1
26/27
Assignment 1
Output:
-
8/10/2019 Wres 1201 Lab1
27/27
.Assignment 1
Due date: 2 weeks from now.
18 March 2014.
Demo in the lab. Write a simple report with a code of the
program.
2 students per group.