DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • Domain-Centric Agile Modeling for Legacy Insurance Systems
  • Securing Kubernetes in Production With Wiz
  • How to Introduce a New API Quickly Using Micronaut
  • Web Crawling for RAG With Crawl4AI
  1. DZone
  2. Data Engineering
  3. Data
  4. Let's Talk ASM - String Concatenation

Let's Talk ASM - String Concatenation

By 
Denzel D. user avatar
Denzel D.
·
May. 03, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
12.8K Views

Join the DZone community and get the full member experience.

Join For Free

not a lot of developers today know assembly, which - regardless of your professional line of work - is a good skill to have. assembly teaches you think on a much lower level, going beyond the abstracted out layer provided by many of the high-level languages. today we're going to look at a way to implement a string concatenation function.

specifically, i want to follow the following procedure for building the final result:

  1. ask the user for input
  2. append a crlf (carriage return + line feed) to the entered string
  3. append the entered string to the existing composite string
  4. follow back from step 1 until the user enters a terminator character
  5. display the composite string
let's assume that you have zero knowledge of assembly. if that is the case, i would recommend starting here . in this example, i am using visual studio 2012 to test the code, but you might as well use an older version of the ide if you want. for convenience purposes, i would recommend downloading the basic framework code that comes for free from the writer of the introduction to 80x86 assembly language and computer architecture book:

  • visual studio 2012
  • visual studio 2010
  • visual studio 2008
first, you have the standard declarations:

.586
.model flat

include io.h            ; header file for input/output
cr      equ     0dh     ; carriage return character
lf      equ     0ah     ; line feed

.stack 4096

.data
prompt      byte   cr, lf, "original string? ",0
restitle    byte   "final result",0
stringin    byte   1024 dup (?)
stringout   byte   1024 dup (?)
linefeed	byte   cr, lf

notice the reference to io.h - at this point you want a way to receive user input and display output data through standard winapi channels, and io.h does just that. some asm experts might argue that it is not a good idea to use winapi hooks in the context of a "pure" assembly program, for educational purposes, but in this situation the focus is on the inner workings of a different function.

note: the program is adapted to the scenario where the execution of the string concatenation function is the sole purpose. as you will get a hang of the execution flow, you can easily adapt it to a scenario where some of the registers can be re-used.

let's start by clearing the ecx and edx registers:

.code
_mainproc proc
		; clear the ecx and edx registers because these will 
		; be used for length counters and sequential increments.
		xor			ecx, ecx
		xor			edx, edx

once the strings will be entered by the user, i will need to find out the length of the string to append, in order to have a correct sequential memory address. now i need to get user input:

input_data:
	; prompt the user to enter the string he ultimately
	; wants appended to the main string buffer.
 input	prompt, stringin, 40    ; read ascii characters

	; make sure that the string doesn't start with the $ character
	; which would automatically mean that we need to terminate the 
	; reading process
	cmp		stringin, '$'
	je		done
	lea		eax, [stringout + edx]  ; destination address
 push	eax			; push the destination on the stack
 lea		eax, [stringin]		; source address
 push	eax			; push the source on the stack
 call	strcopy			; call the string copy procedure

once the string is entered, i can check whether the terminator character - "$", was used.


one of the great things about the cmp instruction is the fact that it checks the starting address of the entered string, therefore i can simply compare the entered data with a single character.

in case the character is encountered, the program flow terminates at done, where the output is displayed:

	done:
		; output the new data.
        output		restitle, stringout 
        mov			eax, 0  
        ret


strcopy is an internal procedure that will simply copy a string from one memory address to another:

strcopy		proc	near32

			push	ebp
			mov		ebp, esp

			push	edi
			push	esi

			pushf

			mov		esi, [ebp+8]
			mov		edi, [ebp+12]

			cld

		whilenonull:
			cmp		byte ptr [esi], 0
			je		endwhilenonull
			movsb
			jmp		whilenonull

		endwhilenonull:
			mov		byte ptr [edi], 0

			popf
			pop		esi
			pop		edi
			pop		ebp
			ret		8
strcopy		endp

to make sure that the next string is properly appended, i need to find out the length of the previous one, for a correct memory address offset:

; let's get the length of the current string - move it
; to the proper register so that we can perform the measurement
mov		edi, eax

; find the length of the string that was just entered
sub		ecx, ecx
sub		al, al
not		ecx
cld
repne	scasb
not		ecx
dec		ecx
add		edx, ecx

repne scasb is used for an in-string iterative null terminator search (you can read more about it here ). it will decrement ecx for each character.

; we need to append the linefeed (crlf) to the string so we apply
; the same string concatenation procedure for that sequence.
lea		eax, [stringout + edx]  ; destination address
push	eax              	; first parameter
lea		eax, [linefeed]    	; source
push	eax              	; second parameter
call	strcopy          	; call string copy procedure
mov		edi, eax

; we know that the crlf characters are 2 entities, therefore
; increment the overall counter by 2.
add		edx, 2

; ask for more input because no terminator character was used.
jmp		input_data

once the basic input data is processed, i can append the crlf sequence and increment edx for the proper offset, after which the program flow is being reset from the point where the user has to enter the next character sequence.

Strings Data Types

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: