Write an Pentium NASM assembly language program to locate a character in a strin
ID: 3688189 • Letter: W
Question
Write an Pentium NASM assembly language program to locate a character in a string.
Read a random string and a character from the input in a main procedure. Use the other procedure to determine if there is such a character in the string. The procedure return yes or no to the main procedure and the main procedure display the result.
pass the parameters using a register.
pass the parameters using the stack.
This is my code:
BUF_LEN EQU 41
%include "io.mac"
.STACK 200H
.DATA
PROMPT_MSG DB "ENTER AN STRING:- ",0
CHAR_MSG DB "ENTER ANY CHARACTER :- ",0
NF_MSG DB "NO, CHARACTER FOUND IN THE GIVEN STRING"
YES_MSG DB "YES. CHARACTER FOUND IN THE GIVEN STRING "
COUNT DB 0
.UPDATA
String RESB BUF_LEN
.CODE
.STARTUP
PutStr PROMPT_MSG
GetStr String,BUF_LEN
mov EBX, String
mov AX,COUNT
PutStr CHAR_MSG
GetCh AL
call check
done:
.EXIT
check:
push EBX
Push AL
push AX
yes_no:
mov SI,[EBX]
cmp AL,[SI]
jne NotFound
inc AX
skip:
inc SI
jmp check
cmp AX,0
je NotFound
mov DL,AX
inc DL
jmp check_done
check_done:
PutStr YES_MSG
PutInt DL
pop EBX
pop AL
leave
ret
NotFound:
PutStr NF_MSG
jmp done
when I run it, DOSBOX showed there are many errors but I couldnot find out them, please check and fix it to me, thank you
Explanation / Answer
Answer:
NASM assembly language program to locate a character in a string :
.Ltext0:
.section .rodata
.LC0:
0000 0A20454E .string " ENTER AN STRING:-"
.LC1:
0014 257300 .string "%s"
.LC2:
0017 0A20454E .string " ENTER ANY CHARACTER :- "
.LC3:
0031 256300 .string "%c"
0034 00000000 .align 8
.LC4:
0038 0A204E4F .string " NO, CHARACTER FOUND IN THE GIVEN STRING"
0062 00000000 .align 8
.LC5:
0068 0A205945 .string " YES. CHARACTER FOUND IN THE GIVEN STRING"
.text
.globl main
main:
.LFB0:
.cfi_startproc
0000 55 pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
0001 4889E5 movq %rsp, %rbp
.cfi_def_cfa_register 6
0004 4883EC30 subq $48, %rsp
0008 64488B04 movq %fs:40, %rax
0011 488945F8 movq %rax, -8(%rbp)
0015 31C0 xorl %eax, %eax
0017 C745D800 movl $0, -40(%rbp)
001e BF000000 movl $.LC0, %edi
0023 B8000000 movl $0, %eax
0028 E8000000 call printf
002d 488D45E0 leaq -32(%rbp), %rax
0031 4889C6 movq %rax, %rsi
0034 BF000000 movl $.LC1, %edi
0039 B8000000 movl $0, %eax
003e E8000000 call __isoc99_scanf
0043 BF000000 movl $.LC2, %edi
0048 B8000000 movl $0, %eax
004d E8000000 call printf
0052 488D45D7 leaq -41(%rbp), %rax
0056 4889C6 movq %rax, %rsi
0059 BF000000 movl $.LC3, %edi
005e B8000000 movl $0, %eax
0063 E8000000 call __isoc99_scanf
0068 C745DC00 movl $0, -36(%rbp)
006f EB1A jmp .L2
.L4:
0071 8B45DC movl -36(%rbp), %eax
0074 4898 cltq
0076 0FB65405 movzbl -32(%rbp,%rax), %edx
007b 0FB645D7 movzbl -41(%rbp), %eax
007f 38C2 cmpb %al, %dl
0081 7504 jne .L3
0083 8345D801 addl $1, -40(%rbp)
.L3:
0087 8345DC01 addl $1, -36(%rbp)
.L2:
008b 8B45DC movl -36(%rbp), %eax
008e 4898 cltq
0090 0FB64405 movzbl -32(%rbp,%rax), %eax
0095 84C0 testb %al, %al
0097 75D8 jne .L4
0099 837DD800 cmpl $0, -40(%rbp)
009d 7511 jne .L5
009f BF000000 movl $.LC4, %edi
00a4 B8000000 movl $0, %eax
00a9 E8000000 call printf
00ae EB0F jmp .L6
.L5:
00b0 BF000000 movl $.LC5, %edi
00b5 B8000000 movl $0, %eax
00ba E8000000 call printf
.L6:
00bf B8000000 movl $0, %eax
00c4 488B4DF8 movq -8(%rbp), %rcx
00c8 6448330C xorq %fs:40, %rcx
00d1 7405 je .L8
00d3 E8000000 call __stack_chk_fail
.L8:
00d8 C9 leave
.cfi_def_cfa 7, 8
00d9 C3 ret
.cfi_endproc
.LFE0:
.Letext0:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.