Friday, September 30, 2016

Load Add and Store Instructions in Assembly Language | Instruction set Architecture

0 comments
We already know (from the previous lectures) how an instruction is formed and what the basic structure of an instruction is. Let’s go through few examples of it.

Instruction Set

An instruction set is composed of two parts:
  1. Opcode
  2. Operand (s)

Opcode

Opcode defines the number of operation supported by this instruction format and used for indexing that operation. For example: 0000 is the opcode for LOADI operation.

Operand (s)

Operands can be a register address, memory address, and immediate value.
In our ISA design, the length of instruction is 16 bits. Such that: 4 bits for opcode, and 12 bits for operand(s).
Opcode (4-bits) Operands (12-bits)
0 3 15

Operations

For this tutorial we will be focusing on the three basic operations, i.e. how to load, add, and store data in assembly language. Following are the few operations along with their opcodes:
OPCODE Operation
0000 LOADI
0001 LOADM
0010 STOREI
0011 STORER
0100 ADDI
0101 ADDR
0110 SUBI
1111
In above operations I is for immediate, M is for memory, and R is for register. Let’s look at the registers and memory first before proceeding to the operations.

Registers

There are 16 registers hence 4 bits are required to address them. Registers are named as R0 to R15.
Register Address Register
0000 R0
0001 R1
0010 R2
0011 R3
0100 R4
0101 R5
0110 R6
1111 R15

Memory

There are 28 memory locations where data may reside. Hence 8 bits are required to address them. Memory locations are named as M0 to M255.
Memory Address Memory
00000000 M0
00000001 M1
00000010 M2
00000011 M3
00000100 M4
00000101 M5
00000110 M6
11111111 M255

LOADM Instruction

Definition

It loads an operand value from a memory location to a register.

Bit allocation

LOADM
REGISTER ADDRESS
MEMORY ADDRESS
0 3 7 15
  • 4 bits for Opcode
  • 4 bits for Register address
  • 8 bits for Memory address

Working

Consider an example:
Assembly Code:                       LOADM   R4    M6
Machine Code:                         0001010000000110
LOADM
R4
M6
In the above instruction operand value i.e. 87 from a memory (M6) copied to a register (R4).

STORER Instruction

Define:

It stores an operand value from a register to a Memory location.

Bit allocation:

STORER
REGISTER ADDRESS
MEMORY ADDRESS
0 3 7 15
  • 4 bits for Opcode
  • 4 bits for Register address
  • 8 bits for Memory address

Working

Consider an example:
Assembly Code:                       STORER   R4    M6
Machine Code:                         0011010000000110
STORER
R4
M6
0 3 7 15
In the above instruction operand value (67) is stored from a register (R4) to a Memory (M6) location.

ADDR Instruction

Define:

It adds register values and keeps the result in the destination register.

Bit allocation:

ADDR
Reg. Address
Reg. Address
0 3 7 11 15
  • 4 bits for Opcode
  • 4 bits for each Register address
    • 4 bits for Source
    • 4 bits for Destination

Working

Consider an example:
Assembly Code:                       ADDR   R2 R1
Machine Code:                         0101 0010 0001 0000
ADDR
R2
R1
0 3 7 11 15

Register R1 and R2 are added i.e. 23 and 79 and there result is then stored into R2 register.

Sample Example

LOADM      R1       M25     ; load from memory M25 and store the value in register R1
LOADM      R2      M69      ; load from memory M69 and store the value in register R2
ADDR         R2       R1          ; add values of register R1 and R2
STORER      R2       M2        ; store the value of register R2 to memory M2

Execution of a Assembly Language Instruction

Our CPU will use three stages for executing a single assembly language instruction. These are:

  1. Fetch:

    CPU fetches the instruction from Instruction memory (Maintained in a 2D Array).

  2. Decode:

    Operation and operands are determined.

  3. Execute:

    Operation is performed on the operands.

To see these steps in practical follow the below program written in c language.

Program

A simulator for the discussed ISA design is written in C language. Click on the below link:

Write A Simulator For The Assembly Language ISA Design In C Language.
Read More..

Tuesday, September 27, 2016

Debugging Assembly Language | Introduction to

9 comments

If you are interested in writing code on command prompt and like writing code in command prompt than this tutorial is for you. Debugging assembly language is easy. Following picture shows an example of it:

What is Debugging?

Debug is a dos program which allows programmers to see execution of the program closely in depth. It is used to:

  • Change and see memory contents
  • Enter and execute programs
  • Hold the program at a specific instruction to check the data values or change them.

Debugging commands Assembly language

Below are the command examples of debugging an assembly language code:

Debug: Enter a program

Q: Quit the program.

R: See register contents/values.

R<register>: see particular register content/value also change it.

Practice Program: See AX register contents and change the value to 2, 34, 543, and GE.

A: this command assembles the program. You can enter assembly instructions into the memory.

A <starting address>: starting address can be an offset of code segment or you can also specify segment register.

Example – your turn: a 100 or a cs: 100 are the same, type a 100 and debug it.

U: a command that un-assembles the program. You can see machine code of your program by using this command. You can also specify the starting and ending addresses of memory section you want to see the code of, U<starting address> (space) <ending address>.

G: G is a go command that allows the program to run until it reaches the breakpoint. At the end register contents are displayed to the programmer.

F: F command is used to fill into the memory. Format of this command is: F<starting address> <ending address> <data>.

E: E command is used to enter any data or change into the memory. For example: E 200 ‘HELLO WORLD’.

D: D command is used to see the register contents. You can also use this command to fill segment providing offset. For example: F 10 10F FF, fills 16bytes with FF.


Read More..

Tuesday, September 20, 2016

Assembly Language Instructions With Examples | Introduction

1 comments

Instructions

Instructions are statements that execute when we assemble the program. The instructions (written in assembly language) are translated into machine language bytes, as we have already discussed in the lecture 02. These machine language instructions/bytes are then executed by the CPU.

Example instruction:       mov       ax, bx

An assembly instruction has 4 basic parts:

  1. Label                     (optional)
  2. Mnemonic          (required)
  3. Operand              (depends on the instruction)
  4. Comment            (optional)

Syntax of assembly language instruction is as follows:

[label:] mnemonic [operands] [;comment]

Note: We will be using Intel IA-32 for this tutorial and onwards.

Labels

There are two types of labels:

  • Data Labels
  • Code Labels

Data Label

Data labels are just like variables in any other language. For example, C language instruction: int myVar 10; we know that myVar is a variable name i.e. data label of int type containing value 10. Similarly, in assembly language we use data labels as variables which hold value (s) for us. For example, count is a variable in the following instruction:

count DWORD 100

Data labels are declared in data segment of the code.

Code Label

Code labels are just the names which are used by other instructions to jump from particular position to that position where data label is located. It must end with a semicolon ( : ). Code labels are declared in code segment. For example:

target:
mov       ax, bx
...
jmp target

target is the data label, which is called by the jmp instruction.

Use: code labels are used as targets for jumping while using loops.

Mnemonics

A small word that acts as an identifier for the instruction. The mnemonics are written in code segment. In following examples mov, sub, add, jmp, call, and mul are the mnemonics:

  • mov       Move/assign one value to another (label)
  • sub         Subtract one value from another
  • add        Adds two values
  • jmp        Jump to a specific location
  • call          Call a procedure/module
  • mul        Multiply two values

Operands

Operand is the remaining part of the instruction. There can be 0 to 3 operands. Where each operand is either a memory operand, a register, an input/output port, or a constant expression.

Following are the operand types:

  • constant expression
  • constant
  • memory (data label)
  • register

Examples

  • 0 operands
  • stc                                          ; set Carry flag
  • 1 operand
  • inc myByte                          ; memory
  • inc eax                                  ; register
  • 2 operands
  • sub myByte,25                  ; memory, constant
  • add ebx,ecx                       ; register, register
  • add eax,36 * 25                 ; register, constant-expression

Comments

You should have used comments in your programs before. As you know there are two types of comments: 1. Single-line and 2. Multi-line comments. In assembly single-line comments begin with ; (semicolon) whereas, multi-line comments begin with a comment directive and symbol/character selected by the programmer itself and also end with the same symbol/character.

Why you should use comments in your programs?

  1. explains the program's purpose
  2. tricky coding techniques
  3. revision information
  4. by whom and when it was written
  5. application-specific explanations
assembly language instructions
Read More..

Monday, September 19, 2016

Assembly Language Fundamentals

0 comments

We need to know few basics of assembly language before we start writing programs in assembly language. The assembly programs run in little memory, consisted of primarily simple operations.

Basic Elements in Assembly Language

Now, let’s discuss the basic elements and syntax of the assembly language, which will help you in writing your first assembly program.

Integer constants

The integer constants consist of leading sign (optional), digits (one or more), and suffix character (optional), the suffix character is also known as radix. The general syntax of an integer constant is:
[+/-] digits [radix]

  • Sign can be negative or positive.
  • Digits can be decimal, binary, octal or hexadecimal digits.
  • Radixcan be d for decimal, b for binary, o/q for octal and h for hexadecimal.

If you do not provide any radix, the number will be considered decimal and if you do not specify any sign, the number will be considered positive by the assembler. Following are the few examples:

Integer Constant Numeric System
26 Decimal
42o Octal
26d Decimal
1Ah Hexadecimal
11010011b Binary
0A3h Hexadecimal
42q Octal

Table 3.1 Integer Constants

Integer Expressions

Combining arithmetic operators (+,-, etc.) and integer values (0, 1, 2…) gives an integer expression, which is also known as a mathematical expression. The integer can be stored in 32 bits (0-FFFFFFFFh). Following are the operator’s w.r.t. their precedence levels:

Operator Name Precedence Level
(  ) Parentheses 1
+, - Unary plus, minus 2
*, / Multiply, Divide 3
MOD Modulus 3
+, - Add, Subtract 4

Examples:

Expression

Value

16/5 3
- (3 + 4) * (6 - 1) -35
- 3 + 4 * 6 - 1 20
25 mod 3 1

Character constants

Declare the character constants with single or double quotes. Each character occupies 1 byte in the memory.
Examples: ‘A’ or “A”, etc.

 String constants

Declare the string constants with single or double quotes. Each character occupies one character, hence the number of characters a string has, is the size of the string.
Examples: ‘Hello World’ or “Hello World”, etc.
The size of the hello world string is 11.

Reserved words

You cannot use few words, known as keywords, when you are programming in any language. These words have special meaning. In assembly language these words are:

  • Mnemonics
  • Directives
  • Type attributes
  • Operators
  • Predefined symbols

Note: mnemonics and directives are discussed below.

Identifiers

You (programmer) can make use of your own words while writing programs called identifier. The identifiers might be:

  • variable
  • constant
  • procedure
  • code label

There are rules that must be followed by an identifier. An identifier:

  1. Should be 1 to 247 characters long.
  2. Is not case sensitive.
  3. First character must be a letter (A...Z, a...z), underscore (_), @, ?, or $.
  4. Subsequent characters may also be digits.
  5. Cannot be the same as an assembler reserved word.

Directives

The commands that are recognized and acted upon by the assembler, known as directives. Directives are not same as instructions, for example: the DWORD directive tells the assembler to reserve space in the memory for a double word variable. But the MOV instruction, on the other hand, executes at runtime, copying the contents of variable (myVar) to the register (EAX) as shown below:
myVar   DWORD        26        ; DWORD directive
mov  eax, myVar                     ; MOV instruction

In next tutorial, we will talk about the instruction set of assembly language.


Read More..

Friday, September 9, 2016

Configure Microsoft Visual Studio for Assembly Language | How to

0 comments
I know you are itching to start writing programs in assembly language, but before we start our fantastic journey of learning how to code in assembly language, there are certain tools that you should have:
  • Assembly Editor
  • Debugging Tool
I personally would suggest Microsoft visual studio (latest version).
Note: For this tutorial we will be using MS-Visual Studio. To download MS-Visual Studio to your computer, visit the following link:
http://www.visualstudio.com/downloads/download-visual-studio-vs
If you have any troubles installing MS-Visual Studio, visit the following link:
http://msdn.microsoft.com/en-us/library/e2h7fzkw.aspx
Now, you have installed the tools required to get going, you should configure it for the assembly language.

Configuration of MS-Visual Studio for MASM

Follow the following steps to configure Microsoft visual studio for assembly language:
  1. Open the Microsoft visual studio from your desktop, which you have installed earlier.
  2. Go to the TOOLS (located at the toolbar) and click on the CUSTOMIZE option. Following window will appear on your screen.
  3. configure microsoft visual studio for assembly language
  4. Click on the Commands tab and then click on the Add Command. You should see the following screen on your monitor/lid.
  5. configure microsoft visual studio for assembly language
  6. Click on the debug option and then select Start/Continue. And click OK.
  7. Now again click on the TOOLS button, and then click on the Options button. New window will appear on your screen as shown in the following figure 3.3.

  8. configure microsoft visual studio for assembly language
  9. Double click on the Text Editor option, and then from the pull-down menu select All Languages and then click on Tabs.
  10. Enter 5 in the text field present in front of the Tab Size and enter 5 in the text field in front of the Indent Size, as shown in the above figure 3.3, and press OK.
Congratulations, now we are ready to write and execute programs in assembly language.

Let’s Run A Program:

  1. Go to the following link to download the zip file. Which will help us later in writing code.
  2. Make a new folder in C drive and rename it to Irvine.
  3. Now copy the downloaded zip file and extract it into the Irvine folder, you should see the following files (fig 3. 4) on your monitor/lid.
  4. run a program in assembly language using microsoft visual studio
    Note: If you don’t have access to C drive, make the new folder on desktop with the name Irvine.
  5. Open MS-Visual Studio and click on the File button. Go to Open and from the pull down menu click on the Project/Solution. A new window will appear on the screen.
  6. From the new window:
    1. Open C drive.
    2. Open Irvine folder
    3. Open Examples folder.
    4. Open ch03 folder.
    5. Open Project folder.
    6. Open Project.sln file.
  7. You should see the following screen on your window. If you don’t see the code on your screen, double click on the AddSum.asm from the solution explorer.

  8. run a program in assembly language using microsoft visual studio
  9. Press ctrl+F5 and you should see the following console window on your screen. Which shows the output of the program written in addsum.asm file.
  10. run a program in assembly language using microsoft visual studio

Read More..

Thursday, September 8, 2016

Advantages and Disadvantages of Assembly Language | Virtual Machines and Machine Levels

6 comments

Advantages and Disadvantages of Assembly Language

Let’s discuss few advantages and disadvantages of a level 3 i.e. assembly language.

Advantages of Assembly Language

  1. Programs written in machine language are replaceable by mnemonics which are easier to remember.
  2. Memory Efficient.
  3. It is not required to keep track of memory locations.
  4. Faster in speed.
  5. Easy to make insertions and deletions.
  6. Hardware Oriented.
  7. Requires fewer instructions to accomplish the same result.

Disadvantages of Assembly Language

  1. Long programs written in such languages cannot be executed on small sized computers.
  2. It takes lot of time to code or write the program, as it is more complex in nature.
  3. Difficult to remember the syntax.
  4. Lack of portability of program between computers of different makes.
  5. No SDKs (System Development Kit).

Concept of Virtual Machines

Virtual machines involves two concepts:
  • Virtual Machines
  • Machine Levels

Virtual Machines

We know that each computer has its own native machine language that effects the hardware of the computer directly, generally known as language L0. It is difficult to write programs in machine language, therefore, a new language could have been constructed, which was easy and more human friendly, called language L1. Following are the two ways to achieve this:
  • Interpretation
  • Translation
Let’s, discuss both ideas one by one:

Interpretation

The program written in L1 is converted into machine language (L0). The relation between L1 and L0 is one-to-one i.e. every instruction of L1 is being interpreted by the L0 and then executed.

Translation

The program of language L1 is completely translated into language L0 and then executed.

Translation of high level language to assembly language

Machine Levels

There are four machine levels as shown in the following figure 2.2:

machine levels of assembly language

High Level Language - Level 4

The programs written in high level (application oriented) languages like: C, C++, Java, etc. are compiled into assembly language.

Assembly Language - Level 3

The relation between assembly language and machine language is one-to-one i.e. every instruction mnemonic of assembly language is being interpreted by the machine language.

Machine Language - Level 2

The level 2 is also known as ISA (Instruction set Architecture) and it is executed by the level 1 i.e. Digital Logic.

Digital Logic - Level 1

The CPU (central processing unit) is being constructed from the digital logic gates, which is part of the computer hardware.

Read More..

Comparison of High Level Language, Assembly Language and Machine Language

0 comments

Relation between Assembly Language and Machine Language

Assembly Language

Machine Language

Assembly language is a numeric language. It is only understood by the systems CPU.

All processors understand same machine language e.g. x86 processors.

It has mnemonics like add, mov, etc.

Every machine language instruction corresponds to the single assembly language instruction.

Table 1.2 relation between AL and ML

Differences between High Level languages and Assembly Language | High Level Language vs Assembly Language

Assembly language is different from higher level languages like java, C++, etc. in many ways, let’s discuss them one by one:

  1. High level languages like java, C++, etc. have one to many relationship with machine code and assembly language. One statement of java or C++ expands into many assembly language commands.
  2. For Example: we have following two instructions in C++ and when they are converted in assembly language, number of instructions is 5. So, there is a one to many relationship for the high level languages with the assembly language.

    C++ code

    Assembly code

    Int varY;
    Int varX = (varY + 14) * 13;

    mov edx, vary
    add edx,14
    mov ecx,13
    imul ecx
    mov varX,edx

    Table 1.2 C++ and assembly language difference

  3. Programs written using high level languages like c#, etc. have the ability to run on just any computer but the assembly language is written for that exact processor family.
  4. High level language is converted into assembly language and then into machine code as shown below in the diagram.
  5. assembly language vs high level language

  6. Assembly language can control the machine code better as compared to high level languages.
  7. Manipulation of bits is easier in assembly language as compared to high level languages.
  8. Assembly language can access any memory but the high level languages can’t.
  9. As high level languages are structures, so they are easy to maintain as compared to assembly language.
  10. In high level languages the executable code is huge and hence it sometimes not efficient. But the assembly language is more efficient as it has smaller executable code.
  11. The assembly language programmer must know about the hardware such as registers, etc. but the high level language programmer doesn’t need to know such kind of stuff.
  12. In assembly language we deal with the pointers at the physical level but in the high level languages the pointers details are hidden.

Applications of Assembly Language | Advantages of Assembly Language

There are number of applications of assembly language:
Different programming languages are used to make different applications or sometimes a part of the application. There are few applications like: hardware interface, device drivers, etc. are written using assembly. But the scientific, commercial, etc. applications are written more efficiently in high level languages. Following are the various applications that are made using assembly language. Hence, it is used:

  1. For writing business apps. Which are used at single/multiple organization platforms.
  2. For drivers which are used in hardware devices like: VGA drivers etc. as they have to effect directly to the computer’s hardware.
  3. In developing games played on computer systems. As, they have to utilize specific amount of memory to deal with a specific event.
  4. Also for embedded systems.
  5. In bios of the system for boot code.

Assembly Language favored over High Level Languages

Assembly Language is favored over a lot of applications as compared to high level languages. Following are the few examples of it:

Hardware device drivers

While writing programs for the drivers of the systems (hardware devices) assembly language is preferred as the assembly can directly access the hardware. As the programs for the drivers are short hence they are fast and effective.

Embedded Systems

Embedded system programs should be written in assembly language so that the program could run fast.

Computer Games

Games require some specific amount of memory locations and the direct access to the computer’s hardware which is provided by the assembly language but not by the high level languages.


Read More..

Wednesday, September 7, 2016

Introduction to Assembly Language

0 comments

Overview

The low level computer programming language is the assembly language and it has direct access to the system’s hardware such as registers, flags, memory locations etc. That is why, you should have some knowledge about the computer hardware/architecture.

In this tutorial, we will be focusing on the assembly programming of x86 microprocessors compatible with the ‘Intel’ instruction architecture (IA-32) that runs on MS-Windows. We will also provide sample codes and execution results for an assembler in tutorial topics i.e. Microsoft Macro Assembler (MASM).

Background You Should Have

You should have knowledge about:

  • Any high level language (C, C++, or Java)
  • Digital logic design
  • And number representations (Binary numbers, decimal numbers, hexadecimal and their conversions).

Stuff You Need Have

You should have an editor to write Assembly Langue code. I personally will recommend ‘Microsoft Visual Studio’ (latest version). The step by step instructions are given here: How to download and configure MS Visual Studio for Assembly Language.

Execution Steps of Assembly Language

The steps that are followed by the system/assembler to create an executable file of the code is as follow:

assembly language execution

Step 1

Text-editor is used by the programmer who produces the source file as an ASCII text file.

Step 2

Source file is then used by the assembler that produces machine language translation of a program known as object file. Other than this it produces the listing file, if any error occurs it goes back to the step1.

Step 3

The linker checks the program in the object file, that whether there is a library call or not. If present, linker provides procedure from the library and combines it to object file. This results in making the execution file.

Step 4

Finally the programs begin to execute when the loader reads the executable file into the memory and pointing the CPU to programs starting address.

Programs You Will Create

You will be able to write programs in two general classes:

  • Real Address Mode (16-bit)
  • Protected Mode (32-bit)

Real Address Mode (16-bit)

Real mode programs run in Console window of the OS (Operating System).

Protected Mode (32-bit)

Protected mode programs run in windows and hence they are easy to write and understand.

You Will Learn

In this tutorial, you will learn:

  • About computer architecture
  • About Boolean logic and its implementation
  •  Management of memory in real and protected mode programs
  • Translation of high level languages like C, C++, Java, etc. to assembly languages and then to machine language.
  • Representation of different types of data like: integer, signed integer, real numbers, etc.
  • Machine level debugging of programs
  • Interaction of the application programs with the operating system, such as interrupt handlers, etc.
  • Creating assembly language programs.
Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact