Hi everyone, I'm new to this site and hope you might be able to help me out?
So I've been given an assignment to produce this output

% ./hw4b foo
here foo and foo there
% ./hw4b ABC123
here ABC123 and ABC123 there
% ./hw4b 'where is'
here where is and where is there

using asm to insert the word/phrase, and only these libraries: stdlib.h, stdio.h, & string.h.
I've already done most of the the C coding (shown below) I think, but I'm stuck on the asm assembler code insertion.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char word[] = "here and there";
int sz;
int wordsz;
char *arg;
char *result;

int main(int argc, char* argv[]) {
   if (argc != 2) {
     printf("Usage: %s takes one string argument.\n", argv[0]);
     exit(1);
   }

   sz = strlen(argv[1]) + 1;
   arg = malloc(sz);
   strcpy(arg, argv[1]);
   wordsz = strlen(word) + 1;
   result = malloc(sz * wordsz);

   __asm__("\n\
	movl sz, %eax\n\
");
   printf("%s\n", result);
   return 0;
}

What I'm stuck on is the actual separating/parsing of the word(s) (delimited by the space ' ' character).. I mean I know that %eax can hold a long word (4 bytes), but if I recall correctly, single chars take up 1 byte each, so there is no register which can hold the entirety of word, nevermind result.

Any help would be greatly appreciated!
Thank you

Recommended Answers

All 5 Replies

Are you required to use inline assembly for this program? It's so simple in C, why do it the hard way??? printf("what is %s and where is %s", argv[1], argv[1]); In any event, you need to use a loop to copy the characters, or you can set up the call to rep movsb.

Yes, I'm required to use asm for that specific function.
What is rep movsb? Never seen it before..

OK, here's what I'm thinking now:

char word[] = "here and there";
int sz;
int wordsz;
char *arg;
char *result;
int iter = 0;
int temp_save = 0;
 
int main(int argc, char* argv[]) {
   ...
   ...
   __asm__("\n\
     \*In here will be a loop which will use iter
       to iterate one char (byte) at a time.
       When it comes upon a space char it will exit
       to the print statement, but when it comes
       upon a newline, it will ret and exit*\
");
   //I'm thinkin of something like this, where temp_save is there
   //to save the index of the beginning of the last word to
   //provide a range ([temp_save,iter]) for strncpy()
   //So essentially this will copy the correct range out of word
   //and append it (followed by the keyword arg) onto result
   strcat(result,"%s %s",strncpy(result,word+temp_save,iter),arg);
   temp_save=iter;

   __asm__("\n\
     \*If newline delimiter not reached, return to
       previous loop*\
");

Herein lies my issue: how can I detect a space (' ') char using assembly?

rep movsb is explained here. If you are going to write inline assembly then you have to know assembly language itself.

Well I have a beginner's knowledge of it..
Can you help me as to my previous question though (comparing characters in assembly)?

you need a loop. In the loop, move one character into the al register then compare al with whatever character you want. The following code isn't compretly correct but will give you an idea of what needs to be done

mov cx,length of string
loop_top:
   mov al,es:[si]; copy one character into al
   inc si;
   cmp al,'D' ; check if the character is the letter 'D'
   je loop_end ; go if it's the letter I want
   loop loop_top ; back to top of loop
loop_end:
   ;; do something here
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.