Assembly Language NASM create a substring
ASSIGNMENT INSTRUCTIONS:
Create the Substring from the Given string, beginIndex and endIndex
The program should create a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring.
Initialize the following values in your program
Given string = “All things bright and beautiful”
beginIndex = 4
endIndex = 20
The program I have the copies a substring from a string is below, I just need to edit it to change the substring to the location and length specified above
CODE I HAVE SO FAR:
section .data
msg db "All things beautiful and bright", 10
msgLen equ $ - msg
section .bss
copiedStr resb msgLen
section .text
global _start
_start:
;; intialization before loop
b2:
mov eax, msg ; eax = msg (address denoted by msg)
mov edx, copiedStr ; edx = copiedStr (address denoted by
copiedStr)
mov ecx, msgLen ; ecx = loop count (length of the char array)
mov esi, 0 ; esi = index (set to 0)
;; array traversal
copyChar:
mov byte bl, [eax + esi] ; char at eax address moved to bl
mov byte [edx + esi], bl ; from bl to the content of
copiedStr
inc esi ; increase the index
loop copyChar ; loop until ecx becomes 0
; print the copiedStr in STD OUT
mov eax, 4
mov ebx, 1
mov ecx, copiedStr
mov edx, msgLen
int 80h
; system exit call
mov eax, 1
mov ebx, 0
int 80h
There are number of things that could be improved with this code.. here some steps
1.Specify which assembler
Unlike C or Python, there are a great many variations in assembler syntax, even for the same architecture, such as the x86 of this code. Generally, it's useful to note which assembler, which target processor and which OS (if any) in the comments at the top of the file. In this case, it looked most like 16-bit TASM, so that's the compiler I used to test this code.
2.Use an ASSUME directive
The code would not assemble for me until I added an ASSUME directive. The ASSUMEdirective doesn't actually generate any code. It simply specifies which assumptions the assembler should make when generating the output. It also helps human readers of your code understand the intended context. In this particular case, I added this line just after the CODE SEGMENT declaration:
ASSUME CS:CODE, DS:DATA, ES:DATA
The CS and DS assumptions are obvious, but the ES assumption is less so. However, the code uses the CMPSBinstruction and based on the context, this means an implicit assumption that ES also points to the DATA segment. In my case, (emulated 16-bit DOS), I had to add a few statements to the start of the code to actually load the DS and ES segment registets approximately
3.Avoid instructions outside any segment
The EXIT code currently looks like this:
EXIT: CODE ENDS END RET
The problem is that the CODE ENDS closes the CODE segment and the END directive tells the assembler that there is no more code and thus the RET instruction may or may not be assembled, and may or may not actually be placed in the CODEsegment. You probably meant instead to do this:
EXIT: RET CODE ENDS END
4.Eliminate convoluting branching
Avoid needless branching. They make your code harder to read and slower to execute. For example, the code currently has this:
JA EXIT JB FIND ; fall through to same length SAMELENGTH: XOR BX,BX ; assume string not found CLD REPE CMPSB JNE EXIT INC BX ; indicate that string was found EXIT:
5.Know your instruction set
The code currently has this set of instructions
DEC DX CMP DX, 0000H JE RED
6.Use REPNE SCASB as appropriate
7.Avoid using SP as a general register
8.Consider using standard length lines
DATA SEGMENT
STR1 DB 'MADAM' LEN1 DW ($-STR1); storing the length of STR1 STR2 DB 'MADAA' LEN2 DW ($-STR2); stroing the length of STR2 DATA ENDS CODE SEGMENT LEA SI, STR1 LEA DI, STR2 MOV DX, LEN1 MOV CX, LEN2 CMP CX, DX; comparing main & substring length JA EXIT; if substring size is bigger than there is no chance to be found it in main string JE SAMELENGTH; if main & sub string both have same length the we can compare them directly JB FIND; general case (substring length < mainstring length): we can apply our main process SAMELENGTH: CLD REPE CMPSB JNE RED JMP GREEN FIND: MOV AL, [SI]; storing the ascii value of current character of mainstring MOV AH, [DI]; storing the ascii value of current character of substring CMP AL,AH; comparing both character JE CHECK; JNE NOTEQL NOTEQL: INC SI; if both character don't match then we would point to the next char of main string DEC DX; DX keeps track of how many character of mainstring is left to process CMP DX, 0000H; checking if there are any character left in the main string for further comparison JE RED; if no character is left in main string then obviously the substring doesn't exists in main string JMP FIND CHECK: MOV CX, LEN2; CX is used internally for REPE CMPSB. So storing length of the substring in CX would limit the number of characters for comparison to exact length of substring. ; For example to compare between "madam" & "ada" we need to compare *ada* portion of main string with substring ada, no more, no less MOV SP, SI; storing the index of current character of main string so if the following REPE CMPSB find mismatch then the process can be started over from the next character of main string (SEE line 1 of TEMPRED) by going to TEMPRED > FIND ADD SP, 0001H CLD REPE CMPSB JNE TEMPRED JMP GREEN TEMPRED:; substring not found starting from the current character of main string, but it is possible to find match if we start from next character in main string MOV SI,SP; going to the next character of main string (after REPE CMPSB of CHECK segment) DEC DX LEA DI, STR2; reloading substring index in DI (after REPE CMPSB of CHECK segment) JMP FIND; if a character matches but the following substring mismatches in main string then we start over the same process from the next character of main string by going to FIND segment GREEN: MOV BX, 0001H; substring found JMP EXIT RED: MOV BX, 0000H; substring not found JMP EXIT EXIT: CODE ENDS END RET
Assembly Language NASM create a substring ASSIGNMENT INSTRUCTIONS: Create the Substring from the Given string, beginIndex...
X86 Assembly Language Help to implement the CipherChar Procedure at the end of the given code INCLUDE Irvine32.inc .data KeyPrompt BYTE "Enter the passphrase: ",0 TextPrompt BYTE "Enter the plaintest: ",0 str1 BYTE "The passphrase has length:",0 str2 BYTE "The plaintest has length:",0 KeyIs BYTE "The passphrase: ",0 PlainTextIs BYTE "The plaintext: ",0 CipherTextIs BYTE "The ciphertext: ",0 KMAX = 64 ; passphrase buffer maximum size BMAX = 128 ; test...
Can someone explain how to get the answer to these two questions please? This is a review for school. ; code fragment V Use this information and the code at the right to answer questions # 20-21. mov edx, OFFSET string mov ecx , MAXSIZE dec ecx call Readstring mov ecx, eax ; number of Given the following declarations for an IA-32 processor MAXSIZE 10 data ; digits entered ;initialize val string BYTE MAXSIZE DUP (?) mov val,0 mov esi,...
Create a flowchart for the following algorithm Program: (exchange.asm) Thia program exchanges every pair of values in an array of even numbe red size model flat,stdcall stack 4096 ExitProcess PROTO, dwExitCode: dword data arrayDW dword 1, .code main PROC ov esi initialize index register with 0(i-0) loop counter mov ecx, LENGTHOF arrayDw) /2 L1 mov eax, arrayDW[esi) mov ebx, arrayDW [esi 41 mov arrayDW[esi], ebx move the item i into EAX 2move item 141 into EBX move item i+1 into...
Complete the following Intel assembly language program which determines whether the byte sized operand stored in memory location 'number' is prime or not. The program will write the value of 0 into the memory location 'answer' if the number is not prime, otherwise the initial value of '1' will be left unmodified (indicating that the number is prime). The program makes use of the DIV instruction to determine the value of quotient and remainder when dividing the number by 2,3,4,......
MASM Assembly Language programming question. I'm having serious issues. I am attempting to scan through a string and replace all the "o" characters with an exclamation mark (!). However my program does not work and no change seems to occur to the string. Also the loop breaks once the first "o" character is encountered.Is there anyone in this Universe that can help me???Please main PROC ;mov edi,OFFSET string mov ecx, LENGTHOF string mov edi,OFFSET string L1:...