Download Assembly Programming - UWC Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Assembly Programming
Notes for Practical 1
mashudu
[email protected]
What is 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 THIS MEAN?
What is Assembly Language?
• designed to make it easier to program in
•
•
•
machine language.
includes short mnemonics (ADD, MOV, SUB,
CALL,etc) which represent machine language
sequences (011000)
"Assembly language has a one-to-one
relationship with machine language..." -"...one
assembly language instruction corresponds to
one machine language instruction".
The lowest form of programming language you
(or anyone SANE) can do on a computer.
eg:
• mov ah, 5 mov ah,
•
•
5 mov ah, 5 mov ah,
5 mov ah, 5 mov ah,
5 mov ah, 5 mov ah,
5 mov ah, 5 mov ah,
5 mov ah, 5 mov ah,
(moves 5 into register ah)
MUCH Easier to understand!
A full program looks like:
What next?
• Saved in a text file named: *.asm; eg: hello.asm.
• What to do with the text file (hello.asm):
• Assembler:
takes programs in Assembly language and
converts them to machine language
(binary,hex,oct,etc).
- takes Hello.asm and produces hello.obj.
Linker:
For use when lots of .asm files must be combined to
produce one (external libraries)
- takes hello.obj and produces hello.exe
Why Assembly language:
• Java:
public class Hello{
public static void main(String[] args){
World");
}
}
or
• C++:
int main(void){
cout << "Hello world\n" << endl;
}
• Java and C++ obviously much easier; so why?
System.out.println("Hello
Reasons
– Optimize speed of critical procedures (1% of program executed 50% of the time
and 10% of program 90%)
• life support systems, hospitals,
• airforce, navy, army
• vehicle computers (airbags)
– Understanding ("Unlocks the secrets of the computer's hardware and software")
• learning to write efficient code.
• y = 5;
• x = (y+4);
• z = 3;
• x = x*z;
• ---> x = (y+4)*3; *
– Unusual or direct interaction with operating system or hardware –
• "raw" disk access, device drivers - antivirus - hdd utils
– Shortage of memory (not such a problem these days)
• microcontrollers/microprocessors
Batch files:
•
•
•
•
•
•
•
A file with an extension of .BAT, containing a list of commands or program routines that the
computer will execute in sequence. www.micro2000uk.co.uk/hardware_glossary.htm
Is an ASCII text file which contains a series of commands. These commands run sequentially.
www.oasismanagement.com/frames/TECHNOLOGY/GLOSSARY/b.html
A program that runs without interactions from a user. boardweb.lausd.k12.ca.us/help/glossary.htm
A file that contains a sequence, or batch, of commands. Batch files are useful for storing sets of
commands that are always executed together because you can simply enter the name of the
batch file instead of entering each command individually.
www.angelfire.com/anime3/internet/opersys.htm
A text file with the extension .BAT that contains DOS commands. When you type in the file name,
DOS carries out the commands contained in the file, in the order they appear in the file. Many
computer programs are installed on a hard disk or "loaded" using batch files, as are many LAN
operating systems. Batch files are used to reduce the amount of repetitive typing required by the
user to start programs and to eliminate the need to remember all of the commands to perform a
task. ... www.courts.state.ny.us/ad4/lib/gloss.html
Summary: a file that contains any number of DOS commands, saved with extension
<filename>.BAT, can will be executed upon typing filename.bat at the command
prompt. Used for reducing typing, creating an automated sequence of instructions.
Possible instructions: copy del move
How to install MASM (Microsoft
Assembler)
• Download the three files at
•
•
•
http://www.cs.uwc.ac.za/~msheikh/COS365Assembler/c
os365.htm to the desktop. Click on masm615.part1.exe
and set Destination folder "C:\"
Make directory: C:\MASM615\pracs
ML /? (lists all possible arguments/switches and what
they do)
Create a batch file:
ASSEMBLE.bat
ML /Zi /Zm /Fm /Fl %1 /link /co (cnt)
C:\MASM615\LIB\irvine16.lib
Segment Registers
• CS Code Segment 16-bit number that
points to the active code-segment
• DS Data Segment 16-bit number that
points to the active data-segment
• SS Stack Segment 16-bit number that
points to the active stack-segment
• ES Extra Segment 16-bit number that
points to the active extra-segment
Pointer Registers
• IP Instruction Pointer 16-bit number that
points to the offset of the next instruction
• SP Stack Pointer 16-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
Another Example
Cont..
•
•
http://www.xs4all.nl/~smit/asm01001.htm#ready
Explanation:
– .model small : Lines that start with a "." are used to provide the assembler with
infomation. 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 C 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.
Cont..
– message db "xxxx" : DB means Define Byte and so it does. In the datasegment 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.
– 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 Efective Address. This intructions 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.
Cont..
– int 21h : This instruction causes an Interrupt. The processor
calls a routine somewhere in memory. 21h tells the processor
what kind of routine, in this case a DOS routine. INT's are very
important and I'll explain more of them later, since they're also
very, very complex. However, for now assume that it just calls a
procedure from DOS. The procedure looks at the AH register
to find out out what it has to do. In this example the value 9 in
the AH register indicates that the procedure should write a bitstring to the screen.
– mov ax, 4c00h : Load the Ax register with the constant value
4c00h
– int 21h : The same INT again. But this time the AH register
contains the value 4ch (AX=4c00h) and to the DOS procedure
that means "exit program". The value of AL is used as an "exitcode" 00h means "No error"