Assembly Language

Download Report

Transcript Assembly Language

CEN 226: Computer Organization
&
Assembly Language :CSC 225
(Lec#4)
By
Dr. Syed Noman
2
Assembly language
• An assembly language is a low-level language
for programming computers.
• It implements a symbolic representation of the
numeric machine codes and other constants needed
to program a particular CPU architecture. This
representation is usually defined by the hardware
manufacturer, and is based on abbreviations (called
mnemonics) that help the programmer remember
individual instructions, registers, etc.
• An assembly language is thus specific to certain
physical or virtual computer architecture (as
opposed to most high-level languages, which are
usually portable).
3
How to learn programming
•
•
•
•
C –Concept
L – Logic thinking
P – Practice
Concept – we must learn the basic syntax,
such as how a program statement is written
• Logic thinking – programming is problem
solving so we must think logically in order to
derive a solution
• Practice – write programs
4
Instruction Set
• A collection of assembly/machine language
instructions that the processor can execute.
• Types of instructions:
▫
▫
▫
▫
▫
Transfer (MOV, PUSH, POP etc)
Arithmetic (ADD, SUB, MUL etc)
Logical (AND, OR, NOT etc
Control Transfer (CALL, JMP etc)
Miscelleneous (NOP, INT, LEA)
5
Format of Assembly language
Statement
• General format for an assembly language
statement
• Label
Instruction
Comment
• Start:
Mov AX, BX
; copy BX into AX
Start is a user defined name.
put in a label in your statement when necessary!!!!
The symbol : is used to indicate that it is a label
In instruction, ‘mov’ is operation code (opcode) and
‘AX’ and ‘BX’ are operands.
6
#-operands instruction
• 2-operands instruction
▫ Mov ax, bx
• 1-operand instruction
▫ Inc dl
• 0-operand instruction
▫ Nop
7
Assignment Statement
•
•
•
•
•
•
•
•
•
•
Assignment
In Java, assignment takes the form:
x = 42 ;
y = 24;
z = x + y;
In assembly language we carry out the same operation
but we use an instruction to denote the assignment
operator (“=” in Java).
mov x, 42
mov y, 24
add z, x
add z, y
8
Assignment Statement
• The mov instruction carries out assignment
in 8086 assembly language.
• It allows us to place a number in a register or in a
memory location (a variable) i.e. it assigns a value to
a register or variable.
• Example: Store the ASCII code for the letter
A in register bx.
• A has ASCII code 65D (01000001B, 41H)
• The following mov instruction carries out the
task:
• mov bx, 65d
9
Assignment Statement
We could also write it as:
mov bx, 41h
or
mov bx, 01000001b
or
mov bx, ‘A’
All of the above are equivalent. They each carry out exactly the same
task, namely the binary number representing the ASCII code of A is
copied into the bx register.
10
Assembly program development
Program
.asm
Object file
.obj
Assemble
link
Executable file
.exe
11
Input and Output in 8086 Assembly
• Each microprocessor provides instructions for I/O with the
devices that are attached to it, e.g. the keyboard and screen.
• The 8086 provides the instructions “in” for input and “out”
for output. These instructions are quite complicated to use, so
we usually use the operating system to do I/O instead.
• The operating system provides a range of I/O subprograms, in
much the same way as there is an extensive library of
subprograms available to the C programmer. In C, to perform
an I/O operation, a subprogram is called using its name, e.g.
putchar() , printf() , getchar() . In addition, for example the
character to be displayed by putchar() is passed as a
parameter to the subprogram e.g. putchar(c).
12
Software Interrupts
• In assembly language we must have a
mechanism to call the operating system to carry
out I/O. In addition we must be able o tell the
operating system what kind of I/O operation we
wish to carry out, e.g. to read a character from
the keyboard, to display a character or string on
the screen or to do disk I/O.
• Finally, we must have a means of passing
parameters to the operating subprogram.
13
“int” instruction
• In 8086 assembly language, operating system
subprograms are not called by name, instead, by using a
software interrupt mechanism An interrupt signals the
processor to suspend its current activity (i.e. running your
program) and to pass control to an interrupt service
program (i.e. part of the operating system).
• A software interrupt is one generated by a program (as
opposed to one generated by hardware). The 8086 int
instruction generates a software interrupt.
• It uses a single operand which is a number indicating
which MSDOS subprogram is to be invoked.
• When the I/O operation is finished, the interrupt service
program terminates and our program will be resumed at
the instruction following int.
14
int 21h and its services
• For I/O and some other operations, the number
used is 21h . Thus, the instruction int 21h transfers
control to the operating system, to a subprogram
that handles I/O operations. This subprogram
handles a variety of I/O operations by calling
appropriate subprograms.
• This means that particular I/O operation (e.g. read a
character, display a character) must be specified.
This is done by placing a specific number in a
register. The ‘ah’ register is used to pass this
information. For example, the subprogram to
display a string is subprogram number 9h.
15
String Output
• Service 9h to display strings which are
terminated by the ‘$’ character.
• In order to use it, we must:
1. Ensure the string is terminated with the ‘$’
character.
2. Specify the starting address of the string in the
dx register.
3. Specify the string output subprogram by storing
9h in ah.
4. Use int 21h to call MS-DOS to execute
subprogram 9h.
16
Defining String Variables
• The following 3 definitions are equivalent ways
of defining a string “abc“:
style1 db “abc” ; string constant
style2 db ‘a’, ‘b’, ‘c’ ; character constants
style3 db 97, 98, 99 ; ASCII codes
17
A program template
.model small
.stack 100h
.code
start:
; < the code/statements are here >
mov ax, 4c00h ; return to OS
int 21h
end start
EP#1: A Simple Assembly Program
.model small
.stack 100h
.data
mesg db “Welcome to Assembly Language $”
.code
start:
mov ax, @data
mov ds, ax
;initialize data segment
mov ah, 9h
lea dx, mesg
int 21h
; service of interrupt 21 to print a message
; load effective address
mov ax, 4c00h
int 21h
; terminating program service
end start
end
19
Program elements
20
Character Output
• The task here is to display a single character on the screen.
There are three elements involved in carrying out this
operation using the int instruction:
1. We specify the character to be displayed. This is done by
storing the character’s ASCII code in a specific 8086
register. In this case we use the dl register, i.e. we use dl to
pass a parameter to the output subprogram.
2. We specify which of MS-DOS’s I/O subprograms we wish to
use. The subprogram to display a character is subprogram
number 2h . This number is stored in the ah register.
3. We request MS-DOS to carry out the I/O operation using
the int instruction. This means that we interrupt our
program and transfer control to the MS-DOS subprogram
that we have specified using the ah register.
21
Character Output
• Example : Write a code fragment to display the
character ’a’ on the screen:
• C version:
putchar( ‘a’ ) ;
• 8086 version:
mov dl, ‘a’ ; dl = ‘a‘
mov ah, 2h ; character output subprogram
int 21h ; call ms-dos output character
• As you can see, this simple task is quite
complicated in assembly language.
22
EP#2: Displaying a character from a
register
.model small
.stack 100h
.data
msg1 db "Displaying a character from register:$”
.code
mov ax, @data
mov ds, ax
mov ah,9 ; display message
mov dx, offset msg1 ; lea dx, msg1
int 21h
mov ah,2 ; display character
mov dl, 65
int 21h
mov ax, 4c00h
int 21h
end
23
EP#3: Displaying a character from a
register on newline
.model small
.stack 100h
.data
msg1 db "Displaying a character from register:”,13,10, ‘$’
.code
mov ax, @data
mov ds, ax
mov ah,9 ; display message
mov dx, offset msg1 ; lea dx, msg1
int 21h
mov ah,2 ; display character
mov dl, 65
int 21h
mov ax, 4c00h
int 21h
end
24
Assignment AP#1, AP#2
1. Write a program to display ‘Salam’ using
(a) character output and
(b) using string output.
2. Write a program to display the message ‘Ding!
Ding! Ding!’ and output ASCII code 7 three
times. (ASCII code 7 is the Bel character. It
causes your machine to beep! ).
25
Character Input with echo
• Example : Write a code fragment to input a
character from the keyboard:
• C version:
getche( ) ;
• 8086 version:
mov ah, 1 ; character input subprogram
int 21h ; call ms-dos output character
• The ASCII code of the character will be moved in
‘al’ register.
26
EP#4: Program to read a character and
display it
.model small
.stack 100h
.code
start:
mov ah, 1h
int 21h
mov dl, al
mov ah, 2h
int 21h
mov ax, 4c00h
int 21h
end start
; keyboard input subprogram
; read character into al
; display subprogram
; display character in dl
; return to ms-dos
27
Assignment: AP#3
3. Write a program to beep, display ‘?’ as a
prompt, read a character and display it on a
new line.
28
EP#5: Input character and display with
messages
Title ask user for 1 character and
display it again on screen with
appropriate messages .
.model small
.stack 100h
.data
msg1 db "Input a character: $"
msg2 db 13,10,"You entered: $"
.code
mov ax, @data
mov ds, ax
mov ah,9 ; display message
lea dx, msg1
int 21h
mov ah, 1
int 21h ; my character in al register
mov bh, al
mov ah,9
lea dx, msg2
int 21h
mov ah,2 ; display character
mov dl, bh
int 21h
mov ax, 4c00h
int 21h
end
29
Character Input without echo
• Example : Write a code fragment to input a
character from the keyboard without displaying it:
• C version:
getch( ) ;
• 8086 version:
mov ah, 8 ; character input subprogram
int 21h ; call ms-dos output character
• The ASCII code of the character will be moved in
‘al’ register.
30
Assignment: AP#4
• Write a program to display a message “Input a
secret character:” and then input the character
and display the character after displaying the
message on newline “hmm.. You actually input:”.