Assembly Programming - University of the Western Cape

Download Report

Transcript Assembly Programming - University of the Western Cape

Assembly Programming
Notes for Practical 1
Adan Patience
Joshua Moran
About the Practicals
• Covering the Assembly Language
• Assembly for Intel-Based Computers
•Fourth edition Kip R. Irvine
•Also the slides help a lot and check website
• 7 Practicals
•Consult Course outline for dates and website for more information
• Practicals 3rd, 4th and 5th period every
Wednesday.
• Homework Due Next Wednesday
• NO late Submissions
• Expect Surprise Tests
Programming Languages
Machine Language
• "... is a numeric language that is specifically understood
•
•
by computer's processor (CPU)..." - consists purely of
numbers:
eg:
1011000000000101 1011000000000101
1011000000000101 1011000000000101
1011000000000101 1011000000000101
1011000000000101 1011000000000101
1011000000000101 1011000000000101
1011000000000101 1011000000000101
1011000000000101
WHAT DOES THAT MEAN?
What is Assembly Language?
• A programming language that is easier for
humans to understand, but slightly less efficient
for computers to process.
• Uses names and hexadecimal pointers instead of
binary numbers.
What is Assembly Language? (cont)
• A machine-orientated language in which
mnemonics are used to represent each
machine-language instruction. E.g. ADD,
MOV, SUB, MUL etc, which represent
machine language sequences (011000)
Translating Languages
English: Display the sum of A times B plus C.
Java: System.out.println(A * B + C);
Assembly Language:
mov ax,A
mul B
add ax,C
call WriteInt
Intel Machine Language:
A1 00000000
F7 25 00000004
03 05 00000008
E8 00500000
• Download Prac 1
• Go to www.cs.uwc.ac.za/~masm/
• Select prac1
• Right click on Masm615.rar and select save link
as
• Save the file to C:/
Starting Dos
• Click On Start
• Then type CMD
• Go to the directory Masm615: cd C:/Masm615/
A full program looks like:
What is a Batch file?
• Similar to a Makefile in unix
• It is a text file with the extension .BAT that contains DOS commands.
• Reduces repetitive typing by executing a batch of commands.
Make32
• Make32.bat
• To Compile: make32.bat “filename” in CMD.
• Note: Remove (omit) the .asm extension
Make32
• Make32 /Zi filename.asm ; include debugging
information
• Make32 /Fl filename.asm ; produce a listing
file (prac1.lst)
• Make32 /Fm filename.asm ; produce a map
file (prac1.map)
• Make32 /Zm filename.asm ; use MASM 5.12
compatibility mode
Make32
• The following command assembles filename.asm
and links filename.obj to the link library
linkfile.lib in the C:\MASM615 directory:
• Make32 /Zi /Zm /Fm /Fl filename.asm
/link /co c:\MASM615\<linkfile>
Hello World
• Open Notepad
• Select File – Save As
• Save as type – All Files
• File name “hello”.asm
• Save the file in prac1
Why Assembly language?
• Java:
public class Hello{
public static void main(String[] args){
System.out.println("Hello World");
}
}
or
• C++:
int main(void){
cout << "Hello world\n" << endl;
}
• Java and C++ obviously much easier; so why?
Hello World
title Hello World program
; ALWAYS include the following lines
; Name: <Dane Brown> (Replace with your own
name)
; Purpose: This program displays "Hello world"
; Date: <Today's date>
(Insert todays date)
include irvine32.inc
; from Irvine CDROM
Hello World
stack 100h
.data
HelloString db "Hello, world", 0 ;message to write
.code
main proc
Hello World
mov edx, offset HelloString
invoke WriteString ; Write to cmd
exit
; irvine.inc: a macro that calls ExitProcess
main endp
end main
Possible Instructions
• Copy
• Move
• Delete (del)
The Basics are over
• The next few slides explain registers in
detail and can be read in your own time.
GOOD LUCK!
Segment Registers
• CS Code Segment 32-bit number that
points to the active code-segment
• DS Data Segment 32-bit number that
points to the active data-segment
• SS Stack Segment 32-bit number that
points to the active stack-segment
• ES Extra Segment 32-bit number that
points to the active extra-segment
Pointer Registers
• IP Instruction Pointer 32-bit number that
points to the offset of the next instruction
• SP Stack Pointer 32-bit number that points
to the offset that the stack is using
• BP Base Pointer used to pass data to and
from the stack
General-Purpose Registers
• AX Accumulator Register mostly used for
calculations and for input/output
• BX Base Register Only register that can be
used as an index
• CX Count Register used for the loop
instruction
• DX Data Register input/output and used
by multiply and divide
Index Registers
• SI Source Index used by string operations
as source
• DI Destination Index used by string
operations as destination
Explanation:
• Explanation:
– .model small : Lines that start with a "." are used to provide the assembler
with information. The word(s) behind it say what kind of info. In this case it just
tells the assembler the program is small and doesn't need a lot of memory. I'll
get back on this later.
– .stack : Another line with info. This one tells the assembler that the "stack"
segment starts here. The stack is used to store temporary data. It isn't used in
the program, but it must be there, because we make an .EXE file and these files
MUST have a stack.
– .data : indicates that the data segment starts here and that the stack segment
ends there.
– .code : indicates that the code segment starts there and the data segment ends
there.
– main proc : Code must be in procedures, just like in Java or any other
language. This indicates a procedure called main starts here. main endp states
that the procedure is finished. Procedures MUST have a start and end. end main
: tells the assembler that the program is finished. It also tells the assembler
were to start the program. At the procedure called main in this case.
Explanation:
– message db "xxxx" : DB means Define Byte and so it does. In
the data-segment it defines a couple of bytes. These bytes
contain the information between the brackets. "Message" is a
name to indentify this byte-string. It's called an "identifier".mov
ax, seg message : AX is a register.
– MOV is an instruction that moves data. It can have a few
"operands". Here the operands are AX and seg message. Seg
message can be seen as a number. It's the number of the
segment "message" is in (The data-segment) We have to know
this number, so we can load the DS register with it. Else we can't
get to the bit-string in memory. We need to know WHERE the
bit-string is located in memory. The number is loaded in the AX
register. MOV always moves data to the operand left of the
comma and from the operand right of the comma.
Explanation:
– mov ds,ax : The MOV instruction again. Here it moves the
number in the AX register (the number of the data segment) into
the DS register. We have to load this DS register this way (with
two instructions) Just typing: "mov ds,segment message" isn't
possible.
– mov ah, 09 : MOV again. This time it load the AH register with
the constant value nine.
– lea dx, message : LEA Load Effective Address. This instructions
stores the offset within the datasegment of the bit-string
message into the DX register. This offset is the second thing we
need to know, when we want to know where "message" is in the
memory. So now we have DS:DX. See the segment explanation
above.