Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Programming Notes for Practical 1

Similar presentations


Presentation on theme: "Assembly Programming Notes for Practical 1"— Presentation transcript:

1 Assembly Programming Notes for Practical 1 mashudumashudun@gmail.com

2 What is Machine Language? "... is a numeric language that is specifically understood by computer's processor (CPU)..." - consists purely of numbers: "... is a numeric language that is specifically understood by computer's processor (CPU)..." - consists purely of numbers: eg: eg: 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 1011000000000101 WHAT DOES THIS MEAN? WHAT DOES THIS MEAN?

3 What is Assembly Language? designed to make it easier to program in machine 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) 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". "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. The lowest form of programming language you (or anyone SANE) can do on a computer.

4 eg: mov ah, 5 mov ah, mov ah, 5 mov ah, 5 mov ah, 5 mov ah, (moves 5 into register ah) (moves 5 into register ah) MUCH Easier to understand! MUCH Easier to understand!

5 A full program looks like:

6 What next? Saved in a text file named: *.asm; eg: hello.asm. Saved in a text file named: *.asm; eg: hello.asm. What to do with the text file (hello.asm): What to do with the text file (hello.asm): Assembler: 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

7 Why Assembly language: Java: Java: public class Hello{ public static void main(String[] args){ System.out.println("Hello World"); }}or C++: C++: int main(void){ cout << "Hello world\n" << endl; } Java and C++ obviously much easier; so why? Java and C++ obviously much easier; so why?

8 Reasons –Optimize speed of critical procedures (1% of program executed 50% of the time and 10% of program 90%) life support systems, hospitals, life support systems, hospitals, airforce, navy, army airforce, navy, army vehicle computers (airbags) vehicle computers (airbags) –Understanding ("Unlocks the secrets of the computer's hardware and software") learning to write efficient code. learning to write efficient code. y = 5; y = 5; x = (y+4); x = (y+4); z = 3; z = 3; x = x*z; x = x*z; ---> x = (y+4)*3; * ---> x = (y+4)*3; * –Unusual or direct interaction with operating system or hardware – "raw" disk access, device drivers - antivirus - hdd utils "raw" disk access, device drivers - antivirus - hdd utils –Shortage of memory (not such a problem these days) microcontrollers/microprocessors microcontrollers/microprocessors

9 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 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 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. board- web.lausd.k12.ca.us/help/glossary.htm A program that runs without interactions from a user. board- web.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 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 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.BAT, can will be executed upon typing filename.bat at the command prompt. Used for reducing typing, creating an automated sequence of instructions. Summary: a file that contains any number of DOS commands, saved with extension.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 Possible instructions: copy del move

10 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:\" 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 Make directory: C:\MASM615\pracs ML /? (lists all possible arguments/switches and what they do) ML /? (lists all possible arguments/switches and what they do) Create a batch file: Create a batch file:ASSEMBLE.bat ML /Zi /Zm /Fm /Fl %1 /link /co (cnt) C:\MASM615\LIB\irvine16.lib

11 Segment Registers CS Code Segment 16-bit number that points to the active code-segment 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 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 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 ES Extra Segment 16-bit number that points to the active extra-segment

12 Pointer Registers IP Instruction Pointer 16-bit number that points to the offset of the next instruction 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 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 BP Base Pointer used to pass data to and from the stack

13 General-Purpose Registers AX Accumulator Register mostly used for calculations and for input/output AX Accumulator Register mostly used for calculations and for input/output BX Base Register Only register that can be used as an index BX Base Register Only register that can be used as an index CX Count Register used for the loop instruction CX Count Register used for the loop instruction DX Data Register input/output and used by multiply and divide DX Data Register input/output and used by multiply and divide

14 Index Registers SI Source Index used by string operations as source SI Source Index used by string operations as source DI Destination Index used by string operations as destination DI Destination Index used by string operations as destination

15 Another Example

16 Cont.. http://www.xs4all.nl/~smit/asm01001.htm#ready http://www.xs4all.nl/~smit/asm01001.htm#ready http://www.xs4all.nl/~smit/asm01001.htm#ready Explanation: 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.

17 Cont.. –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. –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.

18 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 bit- string 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 "exit- code" 00h means "No error"


Download ppt "Assembly Programming Notes for Practical 1"

Similar presentations


Ads by Google