Microprocessor Based Systems: Lecture No 09 Sample Assembly Language Program
Microprocessor Based Systems: Lecture No 09 Sample Assembly Language Program
By Nasir Mahmood
Last Lecture
• IA‐32 Memory Model
– Flat memory model
– Segmented memory model
– Real‐address mode memory model
• IA‐32 Modes of Opera=on
– Real‐address mode
– Protected mode
– Virtual‐8086 mode
– System management mode
• Basic Program Execu=on Registers
– General Purpose registers, Segment registers, Status
registers, instruc=on pointer register
This Lecture
• Sample Assembly Language Programme
• Book Reading “Assembly Language for x86 Processors” 6th
Edi=on By Kip R. Irvine
– Sec=on 3.1 and 3.2
TITLE Program Template (Template.asm)
; Program Description:
; Author, Creation Date, Revisions, Date
INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
4
TITLE Add and Subtract (Template.asm)
; Program Description:
; Author, Creation Date, Revisions, Date
TITLE direc+ve marks the en+re
line as comments
All text on right side of
semicolon is treated as a
comment
5
Direc=ves
• Commands that are recognized and
acted upon by the assembler
– Not part of the Intel instruc=on set
– Used to declare code, data areas, select
memory model, declare procedures, etc.
– not case sensi=ve
• Different assemblers have different
direc=ves
6
TITLE Program Template (Template.asm)
; Program Description,Author, e.t.c
INCLUDE Irvine32.inc
.data
; (insert variables here)
.code
main PROC
Direc+ves
; (insert executable instructions here)
exit
main ENDP
; (insert additional procedures here)
END main
Direc=ves
• INCLUDE Irvine32.inc
– The INCLUDE direc=ve copies necessary
defini=ons and setup informa=on from a text
file named Irvine32.inc
• .code
– The .code direc=ve marks the beginning of the
code segment, where all executable
statements in a program are located.
• main PROC
– The PROC direc=ve iden=fies the beginning of a
procedure. The name of the procedure here is
main.
8
Direc=ves
• Exit
– The exit macro (indirectly) calls a predefined MS‐
Windows func=on that halts the program
• main ENDP
– The ENDP direc=ve marks the end of the main
procedure.
• END main
– The END direc=ve marks the last line of the
program to be assembled. It iden=fies the name
of the program’s startup procedure.
Example: Adding and Subtrac=ng Integers
TITLE Add and Subtract (AddSub.asm)
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
10
Example: Adding and Subtrac=ng Integers
TITLE Add and Subtract (AddSub.asm)
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
11
• main PROC
– The PROC direc=ve iden=fies the beginning of a procedure. The
name of the procedure here is main.
• mov eax,10000h ; EAX = 10000h
– The MOV instruc=on copies the integer 10000h to the EAX
register.
• add eax,40000h ; EAX = 50000h
– The ADD instruc=on adds 40000h to the EAX register.
• sub eax,20000h ; EAX = 30000h
– The SUB instruc=on subtracts 20000h from the EAX register.
• call DumpRegs
– The CALL instruc=on calls a procedure DumpRegs.
Iden=fiers
• An iden=fier is a programmer’s chosen name
• It might iden=fy a variable, a constant, a
procedure or a code label
• Rules
– 1‐247 characters, including digits
– not case sensi=ve
– first character must be a leder, underscore, @, or $
• Examples: main, var1, $first, xVal1
13
Integer Constants
• Op=onal leading + or – sign
• binary, decimal, hexadecimal, or octal
digits
• Common radix characters:
– h – hexadecimal
– d – decimal
– b – binary
– r – encoded real
Examples: 30d, 6Ah, 42, 1101b
Hexadecimal beginning with leder: 0A5h
14
Instruc=ons
• Assembled into machine code by assembler
• Executed at run=me by the CPU
• We use the Intel IA‐32 instruc=on set
• An instruc=on contains:
• Label:‐‐‐‐‐Mnemonic ‐‐‐‐‐‐ Operand‐‐‐‐‐‐
Comment
• (opt.)‐‐‐‐‐(req.)‐‐‐‐‐‐‐‐‐‐‐‐‐‐(depends)‐‐‐‐‐(opt.)
• Target: mov ax, bx ; This is a sample
15
Instruc=ons
• Labels:
– Act as place markers.
– Code label & Data Label
• Instruc=on Mnemonics (Opcode)
– Iden=fies the opera=on carried out by opera=on
• Operands
– Constant, constant expression, register, memory (data
label)
16
Instruc=on 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
17
Example: Adding and Subtrac=ng Integers
TITLE Add and Subtract
(AddSub.asm)
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 18
Example Output
Program output, showing registers and flags:
19
Reserved Words
• Reserved words cannot be used as iden=fiers
– Instruc=on mnemonics
• MOV, ADD
– Direc=ves,
– Adributes
– Operators (+,‐)
– Predefined symbols
• See MASM reference in Appendix D
20
THE END