Lilanka Udawatta
Published © LGPL

Introduction to Assembly language with Freescale Part II

How a simple C code written can be to perform a string reverse function can be written in Assembly language to perform the same function.

IntermediateFull instructions provided30 minutes160
Introduction to Assembly language with Freescale Part II

Things used in this project

Hardware components

FRDM Board
NXP FRDM Board
×1

Hand tools and fabrication machines

NXP CodeWarrior

Story

Read more

Code

Assembly Code for the Swap Operation

Assembly x86
/*************************************************************************
*
* Lab 4 section 2 execise 1
* Funtion string reverse to reverse the string passed
*
* @version 29.10

*************************************************************************/
.text
.align 2
.global stringReverse
.type stringReverse function

/*************************************************************************

* Function String Reverse
* Reverses the string of character passed on from main
* Inputs:
* r0 - address of first charater on string

**************************************************************************
**************************************************************************

* Clears the registers r1, r2, r3
*************************************************************************/

stringReverse:

	mov r1, #0 /* Immediate 0 to r1 */
	mov r2, #0 /* Immediate 0 to r2 */
	mov r3, #0 /* Immediate 0 to r3 */
	push {r1} /* Push r1 onto the memory stack */



/*************************************************************************
* Function pushloop
* Push the string onto the stack in order starting from MSB to LBS

pushloop:
	ldr r1, [r0,r2] /* Load charater of string into r1 */
	push {r1} /* Push the character onto memory stack */
	add r2,r2,#1 /* Increase the count by 1 */
	cmp r1, #0 /* Compare r1 with null to test for end (i.e 0) */
	bne pushloop /* Loop back to pushloop if r1 is not equal to 0 */
	pop {r1} /* Pop r1(null value 0)from stack */



/*************************************************************************
* Function poploop
* Pop's back the string onto the stack in order starting from LSB to MSB
* Reverse order
************************************************************************/

poploop:

	pop {r1} /* Pop the character into r1 in the reverse order*/
	str r1, [r0,r3] /* Store the charater in the registers r0 from start */
	add r3, r3, #1 /* Increase the count of r3 by 1*/
	cmp r2,r3 /*Compare r3 and r2 (number of characters pushed(r2) to number of characters
	popped (r3))*/
	bne poploop /* Branch back to poploop and loop if r2 is not eqaul to r3 */
	bx lr /* Branch back to location stored in lr of main */

Credits

Lilanka Udawatta

Lilanka Udawatta

6 projects • 33 followers
I am Electrical and Electronic engineering student, seeking for opportunities to create something that actually matter to the society.

Comments