SlideShare une entreprise Scribd logo
1  sur  30
Assembly Language Programming


           Chapter 9
• Learning any imperative programming language
  involves mastering a number of common concepts:
• Variables: Declaration/definition
• Assignment: Assigning values to variables
• Input/Output: Displaying messages/displaying variable values
• Control flow: Loops, JUMPs
• Subprograms: Definition and Usage

Programming in assembly language involves mastering
  the same concepts and a few other issues.
Variables

• Here we will simply use the 8086 registers as
  the variables in our programs.
• Registers have predefined names and do not
  need to be declared.
Assignment
• In programming languages such as C/C++/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 (“=”). The above assignments would be
  carried out in 8086 assembly langauge as follows:
• Mov ax, 42
• Add ax, 24
• Mov bx,cx
• The mov instruction carries out assignment.
• It allows us place a number in a register or in a
  memory location (a variable) i.e. it assigns a
  value to a register or a variable.
Example:
• mov bx, ‘A’ To store the ASCII code for the
  letter A in register bx.
• mov bx, 2  Loads the value 2 in to bx
Input/Output
• 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
  for us instead.
• In assembly language we must have a mechanism
  to call the operating system to carry out I/O.
• In addition we must be able to 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, etc…...
• In 8086 assembly language, we do not call operating
  system subprograms by name, instead, we use a
  software interrupt mechanism
• The 8086 INT instruction generates a software interrupt.
• It uses a single operand which is a number indicating
  which MS-DOS subprogram is to be invoked.
• For I/O, 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 you must also specify which I/O
  operation (e.g. read a character, display a character)
  you wish to carry out. This is done by placing a specific
  number in a specific register.
• The ah register is used to pass this information.
• For example, the subprogram to display a
  character is subprogram number 2h.
• This number must be stored in the ah register.
• When the I/O operation is finished, the
  interrupt service program terminates and our
  program will be resumed.
Character Output
• There are three elements involved in carrying out this
  operation using the INT instruction:
• 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.
• 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.
• 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.
• Example : Write a code fragment to display
  the character ’a’ on the screen:

mov dl, ‘a’     ; dl = ‘a‘
mov ah, 2h      ; character output subprogram
int 21h         ; call ms-dos, output character
Character Input
• There are also three elements involved in
  performing character input:
• As for character output, we specify which of MS-
  DOS’s I/O subprograms we wish to use, i.e. the
  character input from the keyboard subprogram. This
  is MS-DOS subprogram number 1h. This number
  must be stored in the ah register.
• We call MS-DOS to carry out the I/O operation using
  the INT instruction as for character output.
• The MS-DOS subprogram uses the al register to
  store the character it reads from the keyboard.
• Example: Write a code fragment to read a
  character from the keyboard

mov ah, 1h     ; keyboard input subprogram
int 21h        ; character input
               ; character is stored in al
• Example: Reading and displaying a character:

  mov ah, 1h    ; keyboard input subprogram
  int 21h       ; read character into al
  mov dl, al    ; copy character to dl
  mov ah, 2h    ; character output subprogram
  int 21h       ; display character in dl
A Complete 8086 Program
           (Using Borland Turbo Assembler)

• Use an editor (notepad) to enter the program into a
  file. We use Borland’s TASM and TLINK commands for
  assembling and linking 8086 assembly language
  programs. TASM program files should have names with
  the extension (3 characters after period) asm.
• Let we will call our first program prog1.asm (You may
  use any name you wish. It is a good practice to choose
  a meaningful file name), which displays the letter ‘a’ on
  the screen. Having entered and saved the program
  using an editor, you must then use the TASM and TLINK
  commands to translate it to machine code in the
  command prompt.
Program 1:
• A complete program to display the letter ‘a’ on the screen:
• To translate the program to machine code and then go for execution,

         C:> tasm prog1.asm

• If you have syntax errors, you will get error messages at this point. You then
  have to edit your program, correct them and repeat the above command.

         C:> tlink prog1

• To execute the program, simply enter the program name and press the Return
  key:

         C:> prog1

• The output of the above program will be in the screen as below:
       a
       C:>
• When a program has finished, we return to the
  operating system.
• Like carrying out an I/O operation, this is also
  accomplished by using the INT instruction. This
  time MS-DOS subprogram number 4c00h is
  used.
• It is the subprogram to terminate a program
  and return to MS-DOS. Hence, the instructions:
  mov ax, 4c00h ; Code for return to MS-DOS
  int 21H        ; Terminates program and return to MS-DOS.

• is a must to terminate a program code, otherwise the program
  may crash
Program 2:
• Write a program to load character ’?’ into register ax and
  display the same on the screen.
Program 3:
•   A program to print a string ‘My First Assembly Language Program’ on the screen
• The above program can be rewritten using the
  instruction LEA as follows;
• Detailed explanation of the above code:
Program 4:
•   Write an interactive ALP to read a byte from the keyboard and display it on the screen
Program 5:
•   Program to list all alphanumeric characters (ASCII Character Set)
    (Illustration for jump instruction)
Lab Exercises Questions
•   Program 6: Program to add any two hexa decimal numbers
•   Program 7: Addition of any two packed BCD numbers.
•   Program 8: Program to subtract packed bcd numbers
•   Program 9: Program to multiply 2 numbers
•   Program 10: Program to divide a packed bcd number.
•   Program 11: A program to list numbers from 0,......., 9 (Illustration of Loop )
•   Program 12: Program to add consecutive 10 numbers
•   Program 13: Program to add any 10 stored numbers
• Program 14: Program to add any 3 numbers, entered through the
  keyboard (User interactive)
• Program 15: A program that prompts the user with the question
  ‘Is it after 12 noon (Y/N)?’ . If the response is ‘y’ for yes , greet the
  user with the message 'Good afternoon, world!’, if the response is
  ‘n’ for No , greet the user with the message 'Good morning,
  world!’,else greet with the default message 'Good day, world!’ in a
  newline. [This program will make use of ‘cmp’, ‘jmp’ instructions]
Assignment Topic


 Core-i7 Processor
Things to remember while writing the assignment

• Assignment should be a hand-written one, with
  maximum neatness.
• It must be submitted on January 1- 2013.
• Late submission will get your hands on a zero mark.
• The assignment should be in A4 size sheets with 4
  pages only (including the cover page).
• The topics included are
   Architecture of Core-i7 (first page)
    Addressing modes of Core-i7 (second page)
    Instruction set of Core-i7 (third page)
Thank You

Contenu connexe

Tendances

Computer organization & architecture chapter-1
Computer organization & architecture chapter-1Computer organization & architecture chapter-1
Computer organization & architecture chapter-1
Shah Rukh Rayaz
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
aviban
 
File access methods.54
File access methods.54File access methods.54
File access methods.54
myrajendra
 
Chapter 2 The 8088 Microprocessor
Chapter 2   The 8088 MicroprocessorChapter 2   The 8088 Microprocessor
Chapter 2 The 8088 Microprocessor
Dwight Sabio
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
Manoj Patil
 

Tendances (20)

Operand and Opcode | Computer Science
Operand and Opcode | Computer ScienceOperand and Opcode | Computer Science
Operand and Opcode | Computer Science
 
Computer organization & architecture chapter-1
Computer organization & architecture chapter-1Computer organization & architecture chapter-1
Computer organization & architecture chapter-1
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
 
Assembly level language
Assembly level languageAssembly level language
Assembly level language
 
Conditional jump
Conditional jumpConditional jump
Conditional jump
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Assembly Language
Assembly LanguageAssembly Language
Assembly Language
 
Computer registers
Computer registersComputer registers
Computer registers
 
File access methods.54
File access methods.54File access methods.54
File access methods.54
 
Chapter 2 The 8088 Microprocessor
Chapter 2   The 8088 MicroprocessorChapter 2   The 8088 Microprocessor
Chapter 2 The 8088 Microprocessor
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Interrupts of 8085
Interrupts of 8085Interrupts of 8085
Interrupts of 8085
 
Introduction for microprocessor
Introduction for microprocessorIntroduction for microprocessor
Introduction for microprocessor
 
DMA operation
DMA operationDMA operation
DMA operation
 

En vedette

8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
Mir Majid
 
Microprocessor and-interfacing-techbymak
Microprocessor and-interfacing-techbymakMicroprocessor and-interfacing-techbymak
Microprocessor and-interfacing-techbymak
Akshay Makadiya
 
1.microprocessor
1.microprocessor1.microprocessor
1.microprocessor
raja p
 

En vedette (17)

Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
 
Pass 1 flowchart
Pass 1 flowchartPass 1 flowchart
Pass 1 flowchart
 
INTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSORINTERRUPTS OF 8086 MICROPROCESSOR
INTERRUPTS OF 8086 MICROPROCESSOR
 
Microprocessor and Interfacing Notes
Microprocessor and Interfacing NotesMicroprocessor and Interfacing Notes
Microprocessor and Interfacing Notes
 
Microprocessor Architecture
Microprocessor ArchitectureMicroprocessor Architecture
Microprocessor Architecture
 
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-2) By Er. Swapnil V. Kaware
 
Microprocessor and-interfacing-techbymak
Microprocessor and-interfacing-techbymakMicroprocessor and-interfacing-techbymak
Microprocessor and-interfacing-techbymak
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Introduction to-microprocessors
Introduction to-microprocessorsIntroduction to-microprocessors
Introduction to-microprocessors
 
1.microprocessor
1.microprocessor1.microprocessor
1.microprocessor
 
Chapter5
Chapter5Chapter5
Chapter5
 
Microprocessor & Interfacing (Part-1) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-1) By Er. Swapnil V. KawareMicroprocessor & Interfacing (Part-1) By Er. Swapnil V. Kaware
Microprocessor & Interfacing (Part-1) By Er. Swapnil V. Kaware
 
Microprocessor & Assembly language by team blackhole
Microprocessor & Assembly language by team blackholeMicroprocessor & Assembly language by team blackhole
Microprocessor & Assembly language by team blackhole
 
Mpmc lab
Mpmc labMpmc lab
Mpmc lab
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 

Similaire à Microprocessor chapter 9 - assembly language programming

cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
kapil078
 
Part III: Assembly Language
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly Language
Ahmed M. Abed
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computer
Martial Kouadio
 

Similaire à Microprocessor chapter 9 - assembly language programming (20)

Assembly Language Programming
Assembly Language ProgrammingAssembly Language Programming
Assembly Language Programming
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
Alp 05
Alp 05Alp 05
Alp 05
 
Chapter1.pdf
Chapter1.pdfChapter1.pdf
Chapter1.pdf
 
EC8691-MPMC-PPT.pptx
EC8691-MPMC-PPT.pptxEC8691-MPMC-PPT.pptx
EC8691-MPMC-PPT.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Lecture01
Lecture01Lecture01
Lecture01
 
Synapseindia dot net development computer programming
Synapseindia dot net development  computer programmingSynapseindia dot net development  computer programming
Synapseindia dot net development computer programming
 
Part III: Assembly Language
Part III: Assembly LanguagePart III: Assembly Language
Part III: Assembly Language
 
Intro To C++ - Class 3 - Sample Program
Intro To C++ - Class 3 - Sample ProgramIntro To C++ - Class 3 - Sample Program
Intro To C++ - Class 3 - Sample Program
 
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part IIIntro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
1.1 programming fundamentals
1.1 programming fundamentals1.1 programming fundamentals
1.1 programming fundamentals
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Foundations of Programming Part I
Foundations of Programming Part IFoundations of Programming Part I
Foundations of Programming Part I
 
Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge
 
Pros and cons of c as a compiler language
  Pros and cons of c as a compiler language  Pros and cons of c as a compiler language
Pros and cons of c as a compiler language
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computer
 

Microprocessor chapter 9 - assembly language programming

  • 2. • Learning any imperative programming language involves mastering a number of common concepts: • Variables: Declaration/definition • Assignment: Assigning values to variables • Input/Output: Displaying messages/displaying variable values • Control flow: Loops, JUMPs • Subprograms: Definition and Usage Programming in assembly language involves mastering the same concepts and a few other issues.
  • 3. Variables • Here we will simply use the 8086 registers as the variables in our programs. • Registers have predefined names and do not need to be declared.
  • 4. Assignment • In programming languages such as C/C++/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 (“=”). The above assignments would be carried out in 8086 assembly langauge as follows: • Mov ax, 42 • Add ax, 24 • Mov bx,cx
  • 5. • The mov instruction carries out assignment. • It allows us place a number in a register or in a memory location (a variable) i.e. it assigns a value to a register or a variable. Example: • mov bx, ‘A’ To store the ASCII code for the letter A in register bx. • mov bx, 2  Loads the value 2 in to bx
  • 6. Input/Output • 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 for us instead. • In assembly language we must have a mechanism to call the operating system to carry out I/O. • In addition we must be able to 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, etc…...
  • 7. • In 8086 assembly language, we do not call operating system subprograms by name, instead, we use a software interrupt mechanism • The 8086 INT instruction generates a software interrupt. • It uses a single operand which is a number indicating which MS-DOS subprogram is to be invoked. • For I/O, 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 you must also specify which I/O operation (e.g. read a character, display a character) you wish to carry out. This is done by placing a specific number in a specific register.
  • 8. • The ah register is used to pass this information. • For example, the subprogram to display a character is subprogram number 2h. • This number must be stored in the ah register. • When the I/O operation is finished, the interrupt service program terminates and our program will be resumed.
  • 9. Character Output • There are three elements involved in carrying out this operation using the INT instruction: • 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. • 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. • 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.
  • 10. • Example : Write a code fragment to display the character ’a’ on the screen: mov dl, ‘a’ ; dl = ‘a‘ mov ah, 2h ; character output subprogram int 21h ; call ms-dos, output character
  • 11. Character Input • There are also three elements involved in performing character input: • As for character output, we specify which of MS- DOS’s I/O subprograms we wish to use, i.e. the character input from the keyboard subprogram. This is MS-DOS subprogram number 1h. This number must be stored in the ah register. • We call MS-DOS to carry out the I/O operation using the INT instruction as for character output. • The MS-DOS subprogram uses the al register to store the character it reads from the keyboard.
  • 12. • Example: Write a code fragment to read a character from the keyboard mov ah, 1h ; keyboard input subprogram int 21h ; character input ; character is stored in al
  • 13. • Example: Reading and displaying a character: mov ah, 1h ; keyboard input subprogram int 21h ; read character into al mov dl, al ; copy character to dl mov ah, 2h ; character output subprogram int 21h ; display character in dl
  • 14. A Complete 8086 Program (Using Borland Turbo Assembler) • Use an editor (notepad) to enter the program into a file. We use Borland’s TASM and TLINK commands for assembling and linking 8086 assembly language programs. TASM program files should have names with the extension (3 characters after period) asm. • Let we will call our first program prog1.asm (You may use any name you wish. It is a good practice to choose a meaningful file name), which displays the letter ‘a’ on the screen. Having entered and saved the program using an editor, you must then use the TASM and TLINK commands to translate it to machine code in the command prompt.
  • 15. Program 1: • A complete program to display the letter ‘a’ on the screen:
  • 16. • To translate the program to machine code and then go for execution, C:> tasm prog1.asm • If you have syntax errors, you will get error messages at this point. You then have to edit your program, correct them and repeat the above command. C:> tlink prog1 • To execute the program, simply enter the program name and press the Return key: C:> prog1 • The output of the above program will be in the screen as below: a C:>
  • 17.
  • 18. • When a program has finished, we return to the operating system. • Like carrying out an I/O operation, this is also accomplished by using the INT instruction. This time MS-DOS subprogram number 4c00h is used. • It is the subprogram to terminate a program and return to MS-DOS. Hence, the instructions: mov ax, 4c00h ; Code for return to MS-DOS int 21H ; Terminates program and return to MS-DOS. • is a must to terminate a program code, otherwise the program may crash
  • 19. Program 2: • Write a program to load character ’?’ into register ax and display the same on the screen.
  • 20. Program 3: • A program to print a string ‘My First Assembly Language Program’ on the screen
  • 21. • The above program can be rewritten using the instruction LEA as follows;
  • 22. • Detailed explanation of the above code:
  • 23.
  • 24. Program 4: • Write an interactive ALP to read a byte from the keyboard and display it on the screen
  • 25. Program 5: • Program to list all alphanumeric characters (ASCII Character Set) (Illustration for jump instruction)
  • 27. Program 6: Program to add any two hexa decimal numbers • Program 7: Addition of any two packed BCD numbers. • Program 8: Program to subtract packed bcd numbers • Program 9: Program to multiply 2 numbers • Program 10: Program to divide a packed bcd number. • Program 11: A program to list numbers from 0,......., 9 (Illustration of Loop ) • Program 12: Program to add consecutive 10 numbers • Program 13: Program to add any 10 stored numbers • Program 14: Program to add any 3 numbers, entered through the keyboard (User interactive) • Program 15: A program that prompts the user with the question ‘Is it after 12 noon (Y/N)?’ . If the response is ‘y’ for yes , greet the user with the message 'Good afternoon, world!’, if the response is ‘n’ for No , greet the user with the message 'Good morning, world!’,else greet with the default message 'Good day, world!’ in a newline. [This program will make use of ‘cmp’, ‘jmp’ instructions]
  • 29. Things to remember while writing the assignment • Assignment should be a hand-written one, with maximum neatness. • It must be submitted on January 1- 2013. • Late submission will get your hands on a zero mark. • The assignment should be in A4 size sheets with 4 pages only (including the cover page). • The topics included are  Architecture of Core-i7 (first page)  Addressing modes of Core-i7 (second page)  Instruction set of Core-i7 (third page)