1 / 28

Assembly Programming

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

becca
Download Presentation

Assembly Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assembly Programming Notes for Practical 1 Adan Patience Joshua Moran

  2. 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

  3. Programming Languages

  4. 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?

  5. 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.

  6. 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)‏

  7. 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

  8. 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:/

  9. Starting Dos • Click On Start • Then type CMD • Go to the directory Masm615: cd C:/Masm615/

  10. A full program looks like:

  11. 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.

  12. Make32 • Make32.bat • To Compile: make32.bat “filename” in CMD. • Note: Remove (omit) the .asm extension

  13. 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

  14. 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>

  15. Hello World • Open Notepad • Select File – Save As • Save as type – All Files • File name “hello”.asm • Save the file in prac1

  16. 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?

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

  18. Hello World stack 100h .data HelloStringdb "Hello, world", 0 ;message to write .code main proc

  19. Hello World movedx, offset HelloString invoke WriteString ; Write to cmd exit ; irvine.inc: a macro that calls ExitProcess main endp end main

  20. Possible Instructions Copy Move Delete (del)‏

  21. The Basics are over • The next few slides explain registers in detail and can be read in your own time. GOOD LUCK!

  22. 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

  23. 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

  24. 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

  25. Index Registers • SI Source Index used by string operations as source • DI Destination Index used by string operations as destination

  26. 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.

  27. 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.

  28. 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.

More Related