0% found this document useful (0 votes)
20 views2 pages

Assignment 1

The document provides instructions for writing an x86/64 assembly language program using NASM on Ubuntu to accept a string and display its length. It explains two methods for determining string length: explicitly storing the length or using a sentinel character. Additionally, it outlines string instructions and their usage with relevant registers for processing strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Assignment 1

The document provides instructions for writing an x86/64 assembly language program using NASM on Ubuntu to accept a string and display its length. It explains two methods for determining string length: explicitly storing the length or using a sentinel character. Additionally, it outlines string instructions and their usage with relevant registers for processing strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Title : Write an X86/64 ALP to accept a string and to display its length.

Environment :

OS : Ubantu,
Assembler : NASM
Linker : ID

Theory :
The variable length strings can have as many characters as required. Generally, we specify
the length of the string by either of the two ways −

• Explicitly storing string length


• Using a sentinel character
We can store the string length explicitly by using the $ location counter symbol that
represents the current value of the location counter. In the following example −
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
$ points to the byte after the last character of the string variable msg. Therefore, $-msg gives
the length of the string. We can also write
msg db 'Hello, world!',0xa ;our dear string
len equ 13 ;length of our dear string
Alternatively, you can store strings with a trailing sentinel character to delimit a string
instead of storing the string length explicitly. The sentinel character should be a special
character that does not appear within a string.
For example −
message DB 'I am loving it!', 0

String Instructions

Each string instruction may require a source operand, a destination operand or both. For 32-
bit segments, string instructions use ESI and EDI registers to point to the source and
destination operands, respectively.
For 16-bit segments, however, the SI and the DI registers are used to point to the source and
destination, respectively.
There are five basic instructions for processing strings. They are −
• MOVS − This instruction moves 1 Byte, Word or Double word of data from memory
location to another.
• LODS − This instruction loads from memory. If the operand is of one byte, it is
loaded into the AL register, if the operand is one word, it is loaded into the AX
register and a double word is loaded into the EAX register.
• STOS − This instruction stores data from register (AL, AX, or EAX) to memory.
• CMPS − This instruction compares two data items in memory. Data could be of a
byte size, word or double word.
• SCAS − This instruction compares the contents of a register (AL, AX or EAX) with
the contents of an item in memory.
Each of the above instruction has a byte, word, and doubleword version, and string
instructions can be repeated by using a repetition prefix.
These instructions use the ES:DI and DS:SI pair of registers, where DI and SI registers
contain valid offset addresses that refers to bytes stored in memory. SI is normally
associated with DS (data segment) and DI is always associated with ES (extra segment).
The DS:SI (or ESI) and ES:DI (or EDI) registers point to the source and destination
operands, respectively. The source operand is assumed to be at DS:SI (or ESI) and the
destination operand at ES:DI (or EDI) in memory.
For 16-bit addresses, the SI and DI registers are used, and for 32-bit addresses, the ESI and
EDI registers are used.

Conclusion : Program is running Successfully o/p is correct.

You might also like