LA3-Accept String and Display Its Length
LA3-Accept String and Display Its Length
LA3-Accept String and Display Its Length
EXP NO: 03
AIM: Write an X86/64 ALP to accept a string and to display its length.
OBJECTIVES:
To learn and understand the operation of accept and display string.
ENVIRONMENT:
Operating System: 64-bit Open source Linux or its derivative.
Programming Tools: Preferably using Linux equivalent or MASM/TASM/NASM/FASM.
Text Editor: geditor
THEORY:
The 80x86 String Instructions: All members of the 80 x 86 families support five different string
instructions: MOVS, CMPS, SCAS, LODS and STOS. They are the string primitives since you
can build most other string operations from these five instructions.
How the String Instructions Operate:The string instructions operate on blocks (contiguous
linear arrays) of memory. For example, the MOVS instruction moves a sequence of bytes from
one memory location to another. The CMPS instruction compares two blocks of memory. The
SCAS instruction scans a block of memory for a particular value. These string instructions often
require three operands a destination block address a source block address and (optionally) an
element count. For example, when using the MOVS instruction to copy a string you need a
source
address a destination address and a count (the number of string elements to move).
Unlike other instructions which operate on memory the string instructions are single-byte
instructions which don't have any explicit operands. The operands for the string instructions
include
For example one variant of the MOVS (move string) instruction copies a string from the source
address specified by DS:SI to the destination address sspecified by ES:DI of length CX.
Likewise,
the CMPS instruction compares the string pointed at by DS:SI of length CX to the string pointed
at by ES: DI.
Not all instructions have source and destination operands (only MOVS and CMPS support
them).
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
For example, the SCAS instruction (scan a string) compares the value in the accumulator to
values in memory. Despite their differences the 80x86's string instructions all have one thing in
common - using them requires that you deal with two segments the data segment and the extra
segment.
The REP/REPE/REPZ and REPNZ/REPNE Prefixes
The string instructions by themselves do not operate on strings of data. The MOVS instruction
for
example will move a single byte word or double word. When executed by itself the MOVS
instruction ignores the value in the CX register. The repeat prefixes tell the 80x86 to do a multi-
byte string operation.
The syntax for the repeat prefix is:
Field: Label repeat mnemonic operand; comment
For MOVS:
REP MOVS
For CMPS:
REPE CMPS REPZ
REPNE REPNZ CMPS
For SCAS:
REPE SCAS
REPZ SCAS
REPNE SCAS
REPNZ SCAS
For STOS:
REP STOS {operands}
You don't normally use the repeat prefixes with the LODS instruction.
As you can see the presence of the repeat prefixes introduces a new field in the source line
- the repeat prefix field. This field appears only on source lines containing string instructions. In
your source file:
The label field should always begin in column one
The repeat field should begin at the first tab stop and
The mnemonic field should begin at the second tab stop.
When specifying the repeat prefix before a string instruction the string instruction repeats CX
times. Without the repeat prefix the instruction operates only on a single byte word or double
word.
You can use repeat prefixes to process entire strings with a single instruction. You can use the
string instructions without the repeat prefix as string primitive operations to synthesize more
powerful string operations.
The operand field is optional. If present MASM simply uses it to determine the size of the string
to
MACRO:
Writing a macro is another way of ensuring modular programming in assembly language.
A macro is a sequence of instructions, assigned by a name and could be used anywhere in the
program.
In NASM, macros are defined with %macro and %endmacro directives.
The macro begins with the %macro directive and ends with the %endmacro directive.
The Syntax for macro definition −
%macro macro_name number_of_params
<macro body>
%endmacro
Where, number_of_params specifies the number parameters, macro_name specifies the name of
the macro.
The macro is invoked by using the macro name along with the necessary parameters. When you
need to use some sequence of instructions many times in a program, you can put those
instructions in a macro and use it instead of writing the instructions all the time.
PROCEDURE:
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
Procedures or subroutines are very important in assembly language, as the assembly language
programs
tend to be large in size. Procedures are identified by a name. Following this name, the body of
the procedure is described which performs a well-defined job. End of the procedure is indicated
by a return statement.
Syntax
Following is the syntax to define a procedure −
proc_name:
procedure body
...
ret
The procedure is called from another function by using the CALL
instruction. The CALL instruction should have the name of the called
procedure as an argument as shown below −
CALL proc_name
The called procedure returns the control to the calling procedure by using the RET instruction.
ALGORITHM:
INPUT: String
OUTPUT: Length of String in hex
STEP 1: Start.
STEP 2: Initialize data section.
STEP 3: Display msg1 on monitor
STEP 4: accept string from user and store it in Rsi Register (Its length gets stored in Rax register
by
default).
STEP 5: Display the result using “display” procedure. Load length of string in data register.
STEP 6. Take counter as 16 int cnt variable
STEP 7: move address of “result” variable into rdi.
STEP 8: Rotate left rbx register by 4 bit.
STEP 15: Loop the statement till counter value becomes zero
STEP 16: Call macro dispmsg and pass result variable and length to it. It will print length of
string.
STEP 17: Return from procedure
STEP 18: Stop
FLOWCHART:
PROGRAM:
section .data
msg1 db 10,13,"Enter a string:"
len1 equ $-msg1
section .bss
str1 resb 200 ;string declaration
result resb 16
section .text
global _start
_start:
;display
mov Rax,1
mov Rdi,1
mov Rsi,msg1
mov Rdx,len1
syscall
;store string
mov rax,0
mov rdi,0
mov rsi,str1
mov rdx,200
syscall
call display
;exit system call
mov Rax ,60
mov Rdi,0
syscall
%macro dispmsg 2
mov Rax,1
mov Rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
display:
mov rbx,rax ; store no in rbx
Progressive Education Society's
Modern College of Engineering, Pune-05.
DEPARTMENT OF COMPUTER ENGINEERING
ret
OUTPUT:
CONCLUSION: