Assignment using the Linux operating system( assembly programming).

Project Description

  1. Implement the strcmp() function. The strcmp() function compares the two strings s1and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. You should write a program that asks for two strings and print out the result of the comparison.

 

  1. Implement a function strreverse() that takes two strings s1 and s2. It should write the reverse of s1 into s2. For example, if s1 is “abc”, then after the function is invoked, s2 has the value of “cba”. Your program should ask for a string and print out its reverse

 

 

 

 

Submission Instructions:

Please submit via email the following:

–          The assembly source code

–          The outputs of the programs

Note on testing of the programs:

As part of testing, you should make sure that your program works for all the cases. Please test your code as thorough as possible.

Solution file consists of 3 files as below 

Driver.c, first.asm and output.txt

Driver.c:

#include <stdio.h>

int strcmp2(const char *s1, const char *s2); // we use strcmp2, because libc already have function with name strcmp

void strreverse(const char *s1, char *s2);

int main()

{

char buffer[40];

printf(“strcmp(“abc”, “abc”) %dn”, strcmp2(“abc”, “abc”));

printf(“strcmp(“acc”, “abc”) %dn”, strcmp2(“acc”, “abc”));

printf(“strcmp(“abb”, “abc”) %dn”, strcmp2(“abb”, “abc”));

strreverse(“hello”, buffer);

printf(“strreverse for “hello” %sn”, buffer);

strreverse(“abc”, buffer);

printf(“strreverse for “abc” %sn”, buffer);

return 0;

}

 

First.asm

 

;

; file: skel.asm

; This file is a skeleton that can be used to start assembly programs.

%include “asm_io.inc”

segment .data

;

; initialized data is put in the data segment here

;

segment .bss

;

; uninitialized data is put in the bss segment

;

segment .text

global _strcmp2 ; this name only for testing, because libc already have function with name strcmp

global _strcmp ; export strcmp function

global _strreverse ; export strreverse function

_strcmp2:

_strcmp:

push edx ; save edx

push ecx ; save ecx

mov eax, [esp + 12] ; load s1 to eax

mov edx, [esp + 16] ; load s2 to edx

strcmp_1:

cmp byte [eax], 0 ; check on zero if (*s1 == ”)

je strcmp_2 ; if zero go out from loop

cmp byte [edx], 0 ; check on zero if (*s2 == ”)

je strcmp_2 ; if zero go out from loop

mov cl, [edx]

cmp [eax], cl ; if (*s1 != *s2)

jne strcmp_2

inc eax ; ++s1

inc edx ; ++s2

jmp strcmp_1 ; go to next iteration

strcmp_2:

mov cl, [eax] ; cl = *s1

sub cl, [edx] ; cl -= *s2

movsx eax, cl ; extend cl to eax

pop ecx ; restore ecx

pop edx ; restore edx

ret

_strreverse:

push eax ; save eax

push edx ; save edx

push ecx ; save ecx

push ebx ; save ebx

mov eax, [esp + 20] ; load s1 to eax

mov edx, eax ; edx = eax

strreverse_1:

cmp byte [edx], 0 ; if (*s1 == ”)

je strreverse_2 ; if zero go out from loop

inc edx ; ++s1

jmp strreverse_1 ; go to next iteration

strreverse_2:

dec edx ; –s1

mov ecx, [esp + 24] ; load s2 to ecx

strreverse_3:

cmp eax, edx ; if (eax > s1), eax – initial pointer to s1

ja strreverse_4 ; if greater go out from loop

mov bl, [edx]

mov [ecx], bl ; *s2 = *s1

dec edx ; –s1

inc ecx ; ++s2

jmp strreverse_3 ; go to next iteration

strreverse_4:

mov byte [ecx], 0

pop ebx ; restore ebx

pop ecx ; restore ecx

pop edx ; restore edx

pop eax ; restore eax

ret

Output.txt:

strcmp(“abc”, “abc”) 0

strcmp(“acc”, “abc”) 1

strcmp(“abb”, “abc”) -1

strreverse for “hello” olleh

strreverse for “abc” cba

Project Details

  • Date April 2, 2015
  • Tags Assembly language

Leave a reply

Your email address will not be published. Required fields are marked *

Back to Top