Sei sulla pagina 1di 27

WRES 1201 Lab

This week:
1. Project requirements
2. Revision
3. Sample code
4. Lab assignment

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.
1st assignment: in the end of this lab session.

Revision
Basic Elements of Assembly Language.
Adding and Subtracting Integers example
of code

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

If no radix: decimal

Examples: 30d, 6Ah, 42, 1101b, 33q, 22o


Hexadecimal beginning with letter must have a leading zero to
prevent the assembler interpreting it as an identifier : 0A5h

Integer Expressions
Operators and precedence levels:

Examples:

Character and String Constants


Enclose character in single or double quotes
'A', "x"
ASCII character = 1 byte

Enclose strings in single or double quotes


"ABC"
'xyz'
Each character occupies a single byte

Embedded quotes:
'Say "Goodnight," Gracie'

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

Identifiers
Identifiers (programmer-chosen name)
May contain 1-247 characters, including digits
Not case-sensitive (by default)
first character must be a letter (A..Z,a..z), _, @, ? or $
Subsequent character may also be a digit
Identifier cannot be the same as assembler reserved word.
Avoid using single @ sign as first character.

Ex : Valid identifier
Var1, Count, MAX, $first, open_file, _12345, @@myfile,_main
common sense create identifier names that easy to understand

Directives
Commands that are recognized / understood and 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 :
.DATA identify area of a program that contains variables
.CODE identify area of a program that contains instructions
PROC identify beginning of a procedure. name PROC

Instructions
Assembled into machine code by 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

A) Labels
Act as place marker marks the address (offset) of code
and data
Follow identifier rules (refer slide pg 8)
Data label
must be unique
example: myArray
mov ax, myVariable
Code label
target of jump and loop instructions target :
mov ax,bx
example: L1:

jmp target

B) Mnemonics and Operands


Instruction Mnemonics is a short word that identifies the operation
carried out by an instruction. (useful name)
Examples :
MOV
ADD
SUB
MUL
INC
DEC
JMP
CALL

move (assign one value to another)


add two values
subtract one value from another
multiply two values
increment
decrement
jump to a new location
call a procedure

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

.. 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

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
!

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

TITLE Kira Baki Biasiswa


; Program Description:
; Author:
; Date Created:
; Last Modification Date:

(pokai.asm)

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

Example: Adding and Subtracting


Integers
TITLE Add and Subtract

(AddSub.asm)

; This program adds and subtracts 32-bit integers.

INCLUDE Irvine32.inc
.code
main PROC
mov eax,10000h
add eax,40000h
sub eax,20000h
call DumpRegs
exit
main ENDP
END main

;
;
;
;

EAX = 10000h
EAX = 50000h
EAX = 30000h
display registers

mov eax,10000h
EAX

0010 0000

EAX

0050 0000

10000 h + 40000 h
= 50000 h

EAX

0030 0000

50000 h 20000 h
= 30000 h

10000 h

add eax,40000h

sub eax,10000h

19

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

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

Suggested Coding Standards (2 of 2)


Indentation and spacing
code and data labels no indentation
executable instructions indent 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

Sample
TITLE Add and Subtract

(AddSubAlt.asm)

; This program adds and subtracts 32-bit integers.


.386
.MODEL flat,stdcall
.STACK 4096

ExitProcess PROTO, dwExitCode:DWORD


DumpRegs PROTO

.code
main PROC
mov eax,10000h
add eax,40000h
sub eax,20000h
call DumpRegs
INVOKE ExitProcess,0
main ENDP
END main

; EAX = 10000h
; EAX = 50000h
; EAX = 30000h

Program Template
TITLE Program Template

(Template.asm)

;
;
;
;
;

INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main

Program Description:
Author:
Creation Date:
Revisions:
Date:
Modified by:

Instruction: please
customize as needed

Assignment 1
Write a program MASM assembly langguage to have the input
and output as follows:
Input:

Assignment 1
Output:

. 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.

Potrebbero piacerti anche