0

i'd like to make a program in assembly which shows the n-th word in a given string for example we run a program

./program 3 'ax ab cd fg'

and we should get

cd   

as a result

here is my code

.intel_syntax noprefix
.text
.globl main

.data
m1 db '' , 0

main:
// compile: gcc prog.s -o prog -m32 -nostdlib

// 0 to eax, ebx
mov eax, 0
mov ebx, 0

// run ./programm 3 'ax ab cd fg'

mov eax , [esp+8] // "argv[1]" -> 3
mov ebx, [esp+12] // "argv[2]" -> 'ax ab cd fg'

cld


search:
// load to AL bite from ESI
lodsb
// check end of string (char '\0')
cmp AL, 0
// if yes -> out
je out
// check if number of spaces == 1?
cmp eax, 1
je nthWord
cmp al, ' '
je space
lodsb // load next bit
jmp search


space:
  dec eax // dec number of spaces 
  jmp search // go back to search

nthWord:
  //copy chars to m1
  //and show var m1 at the console


out:

unfortunately i don't know how to copy chars to a variable m1 and show it and the end

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
FiliusBonacci
  • 104
  • 1
  • 7
  • You don't have to copy characters. You can find the start of the nth word and start outputting each character thereafter until you find a space or null character. – Chai T. Rex Jan 17 '17 at 21:35
  • @ChaiT.Rex how can i output each char until space or \0 ? – FiliusBonacci Jan 17 '17 at 21:46
  • You'd use a loop that checks whether there's a space or null at the beginning of the loop and exits if either are the case. Then, you'd use the proper system call for your operating system that outputs one character. Then, you'd advance to the next character and jump to the beginning of the loop. – Chai T. Rex Jan 17 '17 at 21:55
  • it should run under ubuntu the question is how can i display a single char in a loop? – FiliusBonacci Jan 17 '17 at 22:17
  • See https://stackoverflow.com/questions/20466018/how-to-print-a-single-ascii-char where `ecx` holds the character. – Chai T. Rex Jan 17 '17 at 22:38
  • @ChaiT.Rex it's a different problem. I have my char in AL and like to show it. just added improved version of my code, still get an error – FiliusBonacci Jan 17 '17 at 23:54
  • `m1` is not variable. `m1` is symbol, and value of it is address, where next machine code byte lands during compilation. After `m1` you have `db '', 0` which I honestly don't know how it will compile (the `''` part). Let's pretend it will produce zero. So you define two bytes 0, 0. And `m1` is pointing at the first one. If you will copy string 'veryLongWord', that "buffer" will be exhausted by the first two bytes of string ('v', 'e'), and rest of it will corrupt the memory after that, surprising you later in ugly way. ... The point is there are no variables in ASM. Just bytes in memory. – Ped7g Jan 18 '17 at 09:17
  • @Ped7g ok, leave m1, lets focus on how can I in AWord: print characters because it doesnt work and honestly i have no idea how can i do that – FiliusBonacci Jan 18 '17 at 10:01
  • https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm "sys_write" .. give it address directly into input string, plus the length of word detected. No need to copy anything anywhere, as you have those strings already in memory, and linux sys_write wants length of data separately, so you don't even need to patch the source data with zero terminator, like you would have to with C printf. ... but that `m1` misconception is pretty much summing up your problem, you don't understand the structure of your data, that's why you find it so difficult and confusing, while it's easy. – Ped7g Jan 18 '17 at 10:04
  • Oh wait, you actually do this together with clib, not as pure asm ... then calling sys_write directly is not that clever, find the C wrapper to it (`fwrite`), don't mix C in/out with OS sys_read/sys_write and neither mix with C++ stdin/stdout streams, pick one group, stick to it. (unless you understand what problems you may run into... I actually keep mixing them often, but I know why and how to avoid problems). ... Eventually you may patch the input string by writing zero terminator after the desired word, then `puts`/`printf`. – Ped7g Jan 18 '17 at 10:11

0 Answers0