Monday, September 19, 2016

Assembly Language Fundamentals

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.


No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

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