Sunteți pe pagina 1din 36

Robert Jason D.

Ramos
4-BSECE

MS-DOS
Short for Microsoft Disk Operating System, MS-DOS is a non-graphical command line operating system
derived from 86-DOS that was created for IBM compatible computers. MS-DOS originally written by Tim
Paterson and introduced by Microsoft in August 1981 and was last updated in 1994 when MS-DOS 6.22
was released. MS-DOS allows the user to navigate, open, and otherwise manipulate files on their
computer from a command line instead of a GUI like Windows.

Today, MS-DOS is no longer used; however, the command shell, more commonly known as the
Windows command line is still used by many users. The picture to the right, is an example of a Windows
command line window running in Microsoft Windows 10.

Most computer users are only familiar with how to navigate Microsoft Windows using the mouse. Unlike
Windows, MS-DOS is navigated by using MS-DOS commands. For example, if you wanted to see all the
files in a folder in Windows you would double-click the folder to open the folder in Windows Explorer. In
MS-DOS, you would navigate to the folder using the cd command and then list the files in that folder
using the dir command.
How to use the Windows command line (DOS)

This document covers the basic in navigating and using the Microsoft
Windows command line. On this page, you'll learn how to move around
in the command line, find files, manipulate files, and other important
commands. Keep in mind that there are over 100 different commands
that have been used in MS-DOS and the Windows command line. If
you are interested in learning about the command line in more detail,
see our DOS and command prompt overview, which gives a
description and example for every command.
Get into the Windows command line

Open a Windows command line window by following the steps below. If


you need additional information or alternative methods for all versions
of Windows, see our how to get into DOS and Windows command line
page.
1. Click Start
2. In the Search or Run line, type cmd (short for command), and
press Enter.

Understanding the prompt

After following the above steps, the Windows command line should be
shown (similar to the example below). Typically, Windows starts you at
your user directory. In the example below, the user is Mrhope, so our
prompt is C:\Users\Mrhope>. This prompt tells us we are in the C:
drive (the default drive letter of the hard drive) and currently in the
Mrhope directory, which is a subdirectory of the Users directory.
Key tips
 MS-DOS and the Windows command line are not case sensitive.
 The files and directories shown in Windows are also found in the
command line.
 When working with a file or directory with a space, surround it in
quotes. For example, the directory My Documents would be "My
Documents" when typed.
 File names can have a long file name of 255 characters and a three
character file extension.
 When a file or directory is deleted in the command line, it is not moved
into the Recycle Bin.
 If you need help with any of command type /? after the command. For
example, dir /? would give the options available for the dir command.

Listing the files

Let's learn your first command. Type dir at the prompt to list files in
the current directory. You should get an output similar to the example
image below. Without using any dir options, this is how dir output
appears. As can be seen, you are given lots of useful information
including the creation date and time, directories (<DIR>), and the
name of the directory or file. In the example below, there are 0 files
listed and 14 directories as indicated by the status at the bottom of the
output.
Every command in the command line has options, which are additional
switches and commands that can be added after the command. For
example, with the dir command, you can type dir /p to list the files
and directories in the current directory one page at a time. This switch
is useful to see all the files and directories in a directory that has
dozens or hundreds of files. Each of the command options and switches
is listed in our DOS command overview. We offer guides for individual
commands, as well. For example, if you want to see all the options for
the dir command, refer to our dir command overview for a complete
option listing.
The dir command can also be used to search for specific files and
directories by using wildcards. For example, if you only wanted to list
files or directories that begin with the letter "A" you could type dir a*
to list only the AppData directory, in this above example. See the
wildcard definition for other examples and help with using wildcards.

Moving into a directory

Now that we've seen a list of directories (shown below) in the current
directory, move into one of those directories. To move into a directory,
we use the cd command, so to move into the Desktop type cd desktop
and press enter. Once you've moved into a new directory, the prompt
should change, so in our example, the prompt is now
C:\Users\Mrhope\Desktop>. Now in this desktop directory, see what
files are found in this directory by typing the dir command again.
Understand the files

In the Desktop directory, as shown in the above example, there are 23


files and 7 directories, representing different file types. In Windows,
you are familiar with files having icons that help represent the file type.
In the command line, the same thing is accomplished by the file
extensions. For example, "forum posts.txt" is a text file because it has a
.txt file extension. Time.mp3 is an MP3 music file and minecraft.exe is
an executable file.
 Listing of file extensions and additional help with file extensions.
For most users, you'll only be concerned with executable files, which as
mentioned above, is a file that ends with .exe and are also files that
end with .com and .bat. When the name of these files are typed into
the command line, the program runs, which is the same as double-
clicking a file in Windows. For example, if we wanted to run
minecraft.exe typing "minecraft" at the prompt runs that program.
Note: Keep in mind that if the executable file you are trying to run
is not in the current directory, you'll get an error. Unless you have
set a path for the directory that contains the executable file, which
is how the command line finds external commands.
If you want to view the contents of a file, most versions of the
command line use the edit command. For example, if we wanted to look
at the log file hijackthis.log we would type edit hijackthis.log at the
prompt. For 64-bit versions of Windows that do not support this
command, you can use the start command, for example, type start
notepad hijackthis.log to open the file in Notepad. Further
information about opening and editing a file from the command line can
also be found on the link below.
 How to open and view the contents of a file on a computer.

Moving back a directory

You learned earlier the cd command can move into a directory. This
command also allows you to go back a directory by typing cd.. at the
prompt. When this command is typed, you'll be moved out of the
Desktop directory and back into the user directory. To move back to the
root directory type cd\ to get to the C:\> prompt. If you know the
name of the directory you want to move into, you can also type cd\ and
the directory name. For example, to move into C:\Windows> type
cd\windows at the prompt.

Creating a directory

Now with your basic understanding of navigating the command line let's
start creating new directories. To create a directory in the current
directory, use the mkdir command. For example, create a directory
called "test" by typing mkdir test at the prompt. If created
successfully, you should be returned to the prompt with no error
message. After the directory has been created, move into that directory
with the cd command.
Switching drives

In some circumstances, you may want to copy or list files on another


drive. To switch drives in the Windows command line, type the letter of
the drive followed by a colon. For example, if your CD-ROM drive was
the D drive, you would type d: and press enter. If the drive exists, the
prompt will change to that drive letter.
 How do you copy files from one drive to another drive?
 Additional information and examples of drive letters.

Creating a new file

You can create a new file from the command line using the edit
command, copy con command, or using the start command to open a
file.
 Complete steps on how to create a file in MS-DOS.

Creating a new batch file

In the new test directory let's create your first file. In most
circumstances, you never need to create any file at the command line,
but it is still good to understand how files are created. In this example,
we are creating a batch file. A batch file is a file that ends with .bat and
is a file that can help automate frequently used commands in the
command line. We are calling this batch file example, so type edit
example.bat at the prompt. As mentioned in the document on creating
a file, if the edit command does not work with your version of Windows,
use the start command to open the batch file in Notepad. To perform
this action, you type start notepad example.bat into the prompt.
Both of the above commands open a new blank example.bat window. In
the file, type the below three lines, which clear the screen with the cls
command and then run the dir command.

@echo off
cls
dir

After these three lines have been typed into the file, save, and exit the
file. If you are in the edit command, click File (or press Alt+F) and
then Save. After the file has been saved and you are back into the
command prompt, typing dir should display the example.bat in the test
directory.
Now run the batch file to get a better understanding of what a batch file
does. To run the batch file type example at the prompt, which
executes the batch file and clears the screen and then runs the dir
command.
 Full information and additional examples on batch files.

Moving and copying a file

Now that we've created a file let's move it into an alternate directory.
To help make things easier, create another directory for the files. So,
type mkdir dir2 to create a new directory in the test directory called
dir2. After the new directory has been created, use the move command
to move the example.bat file into that directory. To do this type move
example.bat dir2 at the prompt, if done successfully you should get a
message indicated the file was moved. You could also substitute the
move command for the copy command to copy the file instead of
moving it.

Rename a file

After the file has been moved into the dir2 directory, move into that
directory with the cd command to rename the file. In the dir2 directory,
use the rename command to rename the example file into an alternate
name. Type rename example.bat first.bat at the prompt to rename
the file to first.bat. Now when using the dir command, you should see
the first.bat as the only file.
Tip: When renaming any file make sure the file has the same file
extension. If you were to rename the .bat file to a .txt file, it is no
longer an executable file only a text file. Also, keep in mind that
renaming the file to a different file extension does not convert the
file. For example, if you were to name the file to a .MP3 file it may
look like an MP3 audio file in Windows, but it is not going to play
music.

Deleting a file

Now that we've had our fun with our new file, delete the file with the
del command. Type del first.bat to delete the first.bat file. If
successful, you are returned to the prompt with no errors, and the dir
command shows no files in the current directory.
Tip: When deleting files, you can also use wildcards to delete
multiple files at once. For example, if the directory contained
several .GIF image files you could type del *.gif to delete all files
ending with the .gif file extension.
Renaming a directory

Go back one directory to get back into the test directory by using the
cd.. command mentioned earlier. Now rename our dir2 directory to
something else using the same rename command we used earlier. At
the prompt, type rename dir2 hope to rename the directory to hope.
After this command has been completed, type dir and you should now
see one directory called hope.

Removing a directory

While still in the test directory, remove the hope directory by using the
rmdir command. At the prompt, type rmdir hope to remove the hope
directory.
Tip: If the directory you are trying to remove contains any files or
directories, you'll receive an error. To prevent this error, use the
/s option. For example, if the hope directory still had the first.bat
file, you would need to type rmdir /s hope at the prompt.

Running a program

Any file that is an executable file can be run from the command line by
typing the name of the file. For example, if you listed files using the dir
command and see a file named "myfile.exe" typing "myfile" at the
command line runs that program.

How to list available commands

After getting a good understanding of using the command line from the
steps shown above, you can move on to other available commands by
typing help at the command line. Typing "help" gives you a listing of
available commands with a brief description of each of the commands.
Closing or exiting the command line window

After you are done with the Windows command line, you can type exit
to close the window.

https://www.computerhope.com/jargon/m/msdos.htm
https://www.computerhope.com/issues/chusedos.htm
Robert Jason D. Ramos
4-BSECE
Assembly Language

Assembly language is a low-level programming language for a computer or other programmable


device specific to a particular computer architecture in contrast to most high-level programming
languages, which are generally portable across multiple systems. Assembly language is converted
into executable machine code by a utility program referred to as an assembler like NASM, MASM,
etc.

ARITHMETIC INSTRUCTIONS

The INC Instruction

The INC instruction is used for incrementing an operand by one. It works on a single
operand that can be either in a register or in memory.

Syntax
The INC instruction has the following syntax −

INC destination

The operand destination could be an 8-bit, 16-bit or 32-bit operand.

Example

INC EBX ; Increments 32-bit register

INC DL ; Increments 8-bit register

INC [count] ; Increments the count variable

The DEC Instruction


The DEC instruction is used for decrementing an operand by one. It works on a single
operand that can be either in a register or in memory.
Syntax
The DEC instruction has the following syntax −

DEC destination

The operand destination could be an 8-bit, 16-bit or 32-bit operand.

Example

segment .data

count dw 0

value db 15

segment .text

inc [count]

dec [value]

mov ebx, count

inc word [ebx]

mov esi, value

dec byte [esi]

The ADD and SUB Instructions


The ADD and SUB instructions are used for performing simple addition/subtraction of
binary data in byte, word and doubleword size, i.e., for adding or subtracting 8-bit, 16-
bit or 32-bit operands, respectively.

Syntax
The ADD and SUB instructions have the following syntax −

ADD/SUB destination, source


The ADD/SUB instruction can take place between −

 Register to register

 Memory to register

 Register to memory

 Register to constant data

 Memory to constant data

However, like other instructions, memory-to-memory operations are not possible using
ADD/SUB instructions. An ADD or SUB operation sets or clears the overflow and carry
flags.

Example
The following example will ask two digits from the user, store the digits in the EAX and
EBX register, respectively, add the values, store the result in a memory location 'res'
and finally display the result.

SYS_EXIT equ 1

SYS_READ equ 3

SYS_WRITE equ 4

STDIN equ 0

STDOUT equ 1

segment .data

msg1 db "Enter a digit ", 0xA,0xD

len1 equ $- msg1

msg2 db "Please enter a second digit", 0xA,0xD

len2 equ $- msg2


msg3 db "The sum is: "

len3 equ $- msg3

segment .bss

num1 resb 2

num2 resb 2

res resb 1

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov eax, SYS_WRITE

mov ebx, STDOUT

mov ecx, msg1

mov edx, len1

int 0x80

mov eax, SYS_READ

mov ebx, STDIN

mov ecx, num1

mov edx, 2

int 0x80

mov eax, SYS_WRITE

mov ebx, STDOUT

mov ecx, msg2


mov edx, len2

int 0x80

mov eax, SYS_READ

mov ebx, STDIN

mov ecx, num2

mov edx, 2

int 0x80

mov eax, SYS_WRITE

mov ebx, STDOUT

mov ecx, msg3

mov edx, len3

int 0x80

; moving the first number to eax register and second number to ebx

; and subtracting ascii '0' to convert it into a decimal number

mov eax, [num1]

sub eax, '0'

mov ebx, [num2]

sub ebx, '0'

; add eax and ebx

add eax, ebx

; add '0' to to convert the sum from decimal to ASCII

add eax, '0'


; storing the sum in memory location res

mov [res], eax

; print the sum

mov eax, SYS_WRITE

mov ebx, STDOUT

mov ecx, res

mov edx, 1

int 0x80

exit:

mov eax, SYS_EXIT

xor ebx, ebx

int 0x80

When the above code is compiled and executed, it produces the following result −

Enter a digit:
3
Please enter a second digit:
4
The sum is:
7

The program with hardcoded variables −

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point


mov eax,'3'

sub eax, '0'

mov ebx, '4'

sub ebx, '0'

add eax, ebx

add eax, '0'

mov [sum], eax

mov ecx,msg

mov edx, len

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov ecx,sum

mov edx, 1

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .data

msg db "The sum is:", 0xA,0xD

len equ $ - msg

segment .bss
sum resb 1

When the above code is compiled and executed, it produces the following result −

The sum is:


7

The MUL/IMUL Instruction


There are two instructions for multiplying binary data. The MUL (Multiply) instruction
handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both
instructions affect the Carry and Overflow flag.

Syntax
The syntax for the MUL/IMUL instructions is as follows −

MUL/IMUL multiplier

Multiplicand in both cases will be in an accumulator, depending upon the size of the
multiplicand and the multiplier and the generated product is also stored in two registers
depending upon the size of the operands. Following section explains MUL instructions
with three different cases −

Sr.No. Scenarios

When two bytes are multiplied −

The multiplicand is in the AL register, and the multiplier is a byte in the memory or in another register.

1 The product is in AX. High-order 8 bits of the product is stored in AH and the low-order 8 bits are
stored in AL.

When two one-word values are multiplied −

The multiplicand should be in the AX register, and the multiplier is a word in memory or another
2
register. For example, for an instruction like MUL DX, you must store the multiplier in DX and the
multiplicand in AX.
The resultant product is a doubleword, which will need two registers. The high-order (leftmost) portion
gets stored in DX and the lower-order (rightmost) portion gets stored in AX.

When two doubleword values are multiplied −

When two doubleword values are multiplied, the multiplicand should be in EAX and the multiplier is a
doubleword value stored in memory or in another register. The product generated is stored in the
3 EDX:EAX registers, i.e., the high order 32 bits gets stored in the EDX register and the low order 32-
bits are stored in the EAX register.

Example

MOV AL, 10

MOV DL, 25

MUL DL

...

MOV DL, 0FFH ; DL= -1

MOV AL, 0BEH ; AL = -66

IMUL DL

Example
The following example multiplies 3 with 2, and displays the result −

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov al,'3'

sub al, '0'


mov bl, '2'

sub bl, '0'

mul bl

add al, '0'

mov [res], al

mov ecx,msg

mov edx, len

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov ecx,res

mov edx, 1

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .data

msg db "The result is:", 0xA,0xD

len equ $- msg

segment .bss

res resb 1
When the above code is compiled and executed, it produces the following result −

The result is: 6

The DIV/IDIV Instructions


The division operation generates two elements - a quotient and a remainder. In case
of multiplication, overflow does not occur because double-length registers are used to
keep the product. However, in case of division, overflow may occur. The processor
generates an interrupt if overflow occurs.

The DIV (Divide) instruction is used for unsigned data and the IDIV (Integer Divide) is
used for signed data.

Syntax
The format for the DIV/IDIV instruction −

DIV/IDIV divisor

The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit or
32-bit operands. The operation affects all six status flags. Following section explains
three cases of division with different operand size −

Sr.No. Scenarios

When the divisor is 1 byte −

The dividend is assumed to be in the AX register (16 bits). After division, the quotient goes to the AL
register and the remainder goes to the AH register.

When the divisor is 1 word −

The dividend is assumed to be 32 bits long and in the DX:AX registers. The high-order 16 bits are in
2
DX and the low-order 16 bits are in AX. After division, the 16-bit quotient goes to the AX register and
the 16-bit remainder goes to the DX register.
When the divisor is doubleword −

The dividend is assumed to be 64 bits long and in the EDX:EAX registers. The high-order 32 bits are
in EDX and the low-order 32 bits are in EAX. After division, the 32-bit quotient goes to the EAX
register and the 32-bit remainder goes to the EDX register.

Example
The following example divides 8 with 2. The dividend 8 is stored in the 16-bit AX
register and the divisor 2 is stored in the 8-bit BL register.

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov ax,'8'

sub ax, '0'

mov bl, '2'

sub bl, '0'

div bl
add ax, '0'

mov [res], ax

mov ecx,msg

mov edx, len

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov ecx,res

mov edx, 1

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .data

msg db "The result is:", 0xA,0xD

len equ $- msg

segment .bss

res resb 1

When the above code is compiled and executed, it produces the following result −

The result is:


4
LOGIC INSTRUCTIONS

The processor instruction set provides the instructions AND, OR, XOR, TEST, and NOT
Boolean logic, which tests, sets, and clears the bits according to the need of the program.

The format for these instructions −

Sr.No. Instruction Format

1 AND AND operand1, operand2

2 OR OR operand1, operand2

3 XOR XOR operand1, operand2

4 TEST TEST operand1, operand2

5 NOT NOT operand1

The first operand in all the cases could be either in register or in memory. The second
operand could be either in register/memory or an immediate (constant) value. However,
memory-to-memory operations are not possible. These instructions compare or match
bits of the operands and set the CF, OF, PF, SF and ZF flags.

The AND Instruction


The AND instruction is used for supporting logical expressions by performing bitwise
AND operation. The bitwise AND operation returns 1, if the matching bits from both the
operands are 1, otherwise it returns 0. For example −

Operand1: 0101
Operand2: 0011
----------------------------
After AND -> Operand1: 0001

The AND operation can be used for clearing one or more bits. For example, say the BL
register contains 0011 1010. If you need to clear the high-order bits to zero, you AND
it with 0FH.
AND BL, 0FH ; This sets BL to 0000 1010

Let's take up another example. If you want to check whether a given number is odd or
even, a simple test would be to check the least significant bit of the number. If this is 1,
the number is odd, else the number is even.

Assuming the number is in AL register, we can write −

AND AL, 01H ; ANDing with 0000 0001

JZ EVEN_NUMBER

The following program illustrates this −

Example

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov ax, 8h ;getting 8 in the ax

and ax, 1 ;and ax with 1

jz evnn

mov eax, 4 ;system call number (sys_write)

mov ebx, 1 ;file descriptor (stdout)

mov ecx, odd_msg ;message to write

mov edx, len2 ;length of message

int 0x80 ;call kernel

jmp outprog

evnn:

mov ah, 09h

mov eax, 4 ;system call number (sys_write)


mov ebx, 1 ;file descriptor (stdout)

mov ecx, even_msg ;message to write

mov edx, len1 ;length of message

int 0x80 ;call kernel

outprog:

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .data

even_msg db 'Even Number!' ;message showing even number

len1 equ $ - even_msg

odd_msg db 'Odd Number!' ;message showing odd number

len2 equ $ - odd_msg

When the above code is compiled and executed, it produces the following result −

Even Number!

Change the value in the ax register with an odd digit, like −

mov ax, 9h ; getting 9 in the ax

The program would display:

Odd Number!

Similarly to clear the entire register you can AND it with 00H.
The OR Instruction
The OR instruction is used for supporting logical expression by performing bitwise OR
operation. The bitwise OR operator returns 1, if the matching bits from either or both
operands are one. It returns 0, if both the bits are zero.

For example,

Operand1: 0101
Operand2: 0011
----------------------------
After OR -> Operand1: 0111

The OR operation can be used for setting one or more bits. For example, let us assume
the AL register contains 0011 1010, you need to set the four low-order bits, you can OR
it with a value 0000 1111, i.e., FH.

OR BL, 0FH ; This sets BL to 0011 1111

Example
The following example demonstrates the OR instruction. Let us store the value 5 and 3
in the AL and the BL registers, respectively, then the instruction,

OR AL, BL

should store 7 in the AL register −

section .text

global _start ;must be declared for using gcc

_start: ;tell linker entry point

mov al, 5 ;getting 5 in the al

mov bl, 3 ;getting 3 in the bl

or al, bl ;or al and bl registers, result should be 7

add al, byte '0' ;converting decimal to ascii


mov [result], al

mov eax, 4

mov ebx, 1

mov ecx, result

mov edx, 1

int 0x80

outprog:

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .bss

result resb 1

When the above code is compiled and executed, it produces the following result −

The XOR Instruction


The XOR instruction implements the bitwise XOR operation. The XOR operation sets the
resultant bit to 1, if and only if the bits from the operands are different. If the bits from
the operands are same (both 0 or both 1), the resultant bit is cleared to 0.

For example,

Operand1: 0101
Operand2: 0011
----------------------------
After XOR -> Operand1: 0110

XORing an operand with itself changes the operand to 0. This is used to clear a register.

XOR EAX, EAX


The TEST Instruction
The TEST instruction works same as the AND operation, but unlike AND instruction, it
does not change the first operand. So, if we need to check whether a number in a
register is even or odd, we can also do this using the TEST instruction without changing
the original number.

TEST AL, 01H


JZ EVEN_NUMBER

The NOT Instruction


The NOT instruction implements the bitwise NOT operation. NOT operation reverses the
bits in an operand. The operand could be either in a register or in the memory.

For example,

Operand1: 0101 0011


After NOT -> Operand1: 1010 1100
ADDRESSING MODES
Most assembly language instructions require operands to be processed. An operand
address provides the location, where the data to be processed is stored. Some
instructions do not require an operand, whereas some other instructions may require
one, two, or three operands.

When an instruction requires two operands, the first operand is generally the destination,
which contains data in a register or memory location and the second operand is the
source. Source contains either the data to be delivered (immediate addressing) or the
address (in register or memory) of the data. Generally, the source data remains
unaltered after the operation.

The three basic modes of addressing are −

 Register addressing

 Immediate addressing

 Memory addressing

Register Addressing
In this addressing mode, a register contains the operand. Depending upon the
instruction, the register may be the first operand, the second operand or both.

For example,

MOV DX, TAX_RATE ; Register in first operand

MOV COUNT, CX ; Register in second operand

MOV EAX, EBX ; Both the operands are in registers

As processing data between registers does not involve memory, it provides fastest
processing of data.

Immediate Addressing
An immediate operand has a constant value or an expression. When an instruction with
two operands uses immediate addressing, the first operand may be a register or memory
location, and the second operand is an immediate constant. The first operand defines
the length of the data.

For example,

BYTE_VALUE DB 150 ; A byte value is defined

WORD_VALUE DW 300 ; A word value is defined

ADD BYTE_VALUE, 65 ; An immediate operand 65 is added

MOV AX, 45H ; Immediate constant 45H is transferred to AX

Direct Memory Addressing


When operands are specified in memory addressing mode, direct access to main
memory, usually to the data segment, is required. This way of addressing results in
slower processing of data. To locate the exact location of data in memory, we need the
segment start address, which is typically found in the DS register and an offset value.
This offset value is also called effective address.

In direct addressing mode, the offset value is specified directly as part of the instruction,
usually indicated by the variable name. The assembler calculates the offset value and
maintains a symbol table, which stores the offset values of all the variables used in the
program.

In direct memory addressing, one of the operands refers to a memory location and the
other operand references a register.

For example,

ADD BYTE_VALUE, DL ; Adds the register in the memory location

MOV BX, WORD_VALUE ; Operand from the memory is added to register

Direct-Offset Addressing
This addressing mode uses the arithmetic operators to modify an address. For example,
look at the following definitions that define tables of data −

BYTE_TABLE DB 14, 15, 22, 45 ; Tables of bytes


WORD_TABLE DW 134, 345, 564, 123 ; Tables of words

The following operations access data from the tables in the memory into registers −

MOV CL, BYTE_TABLE[2] ; Gets the 3rd element of the BYTE_TABLE

MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE

MOV CX, WORD_TABLE[3] ; Gets the 4th element of the WORD_TABLE

MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE

Indirect Memory Addressing


This addressing mode utilizes the computer's ability of Segment:Offset addressing.
Generally, the base registers EBX, EBP (or BX, BP) and the index registers (DI, SI),
coded within square brackets for memory references, are used for this purpose.

Indirect addressing is generally used for variables containing several elements like,
arrays. Starting address of the array is stored in, say, the EBX register.

The following code snippet shows how to access different elements of the variable.

MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0

MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX

MOV [EBX], 110 ; MY_TABLE[0] = 110

ADD EBX, 2 ; EBX = EBX +2

MOV [EBX], 123 ; MY_TABLE[1] = 123

The MOV Instruction


We have already used the MOV instruction that is used for moving data from one storage
space to another. The MOV instruction takes two operands.

Syntax
The syntax of the MOV instruction is −

MOV destination, source

The MOV instruction may have one of the following five forms −
MOV register, register

MOV register, immediate

MOV memory, immediate

MOV register, memory

MOV memory, register

Please note that −

 Both the operands in MOV operation should be of same size

 The value of source operand remains unchanged

The MOV instruction causes ambiguity at times. For example, look at the statements −

MOV EBX, [MY_TABLE] ; Effective Address of MY_TABLE in EBX

MOV [EBX], 110 ; MY_TABLE[0] = 110

It is not clear whether you want to move a byte equivalent or word equivalent of the
number 110. In such cases, it is wise to use a type specifier.

Following table shows some of the common type specifiers −

Type Specifier Bytes addressed

BYTE 1

WORD 2

DWORD 4

QWORD 8

TBYTE 10

Example
The following program illustrates some of the concepts discussed above. It stores a
name 'Zara Ali' in the data section of the memory, then changes its value to another
name 'Nuha Ali' programmatically and displays both the names.
section .texts

global_start ;must be declared for linker (ld)

_start: ;tell linker entry point

;writing the name 'Zara Ali'

mov edx,9 ;message length

mov ecx, name ;message to write

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov [name], dword 'Nuha' ; Changed the name to Nuha Ali

;writing the name 'Nuha Ali'

mov edx,8 ;message length

mov ecx,name ;message to write

mov ebx,1 ;file descriptor (stdout)

mov eax,4 ;system call number (sys_write)

int 0x80 ;call kernel

mov eax,1 ;system call number (sys_exit)

int 0x80 ;call kernel

section .data

name db 'Zara Ali '

When the above code is compiled and executed, it produces the following result –

Zara Ali Nuha Ali

S-ar putea să vă placă și