-1

I am new to assembly, so sorry if the question is quite obvious.I am trying to solve a assembly program for which i am required to parse a string character by character and do some operations on the character and store it in another string.So my question is how to parse a string character by character in nasm assembly.

Vinod Kumar
  • 71
  • 1
  • 6

1 Answers1

3

do something like this:

    mov si, <adress of the string in memory>
    mov cx, <whatever value tells you how long the string is>
    cld      ; make sure lodsb walks forward, not back (that'd be std then)
again: 
    lodsb    ; get next char to AL and increase SI
    ;
    ; <al contains each char of string here, one by one>
    ;
    dec cx
    jnz again   ; same as "loop", but not so small
Tommylee2k
  • 2,683
  • 1
  • 9
  • 22
  • 1
    The question was tagged NASM, so writing `mov cx, stringLen` with put the OP in trouble. Maybe correct or state that you wrote it for MASM. – Fifoernik Mar 18 '16 at 17:49
  • 1
    If you're going to use a slow instruction like `loop`, you might as well go all out for code-size instead of speed and use `lodsb` instead of `mov` / `inc si`. – Peter Cordes Mar 18 '16 at 21:50
  • @Peter is "loop" it still slow? i have in mind i read some release notes where they fixed it ... or was it that they will not fixed it, for no one uses it? :P no idea :) i hope for this case, +- some cpu cycles won't matter – Tommylee2k Mar 21 '16 at 10:30
  • @Fifoernik since he didn't show us how his sting is stored, and where he's getting the information from, I cannot "correct" anything. probably the string is in BX, and the size in AX ? or it's a constant in memory? who knows ... I cannot predict what he wants, if he doesnt show us at least the minimum code needed – Tommylee2k Mar 21 '16 at 10:34
  • [`loop` is still slow, except on AMD Bulldozer-family](http://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently). I dug up some plausible answers and interesting history in that Q&A. Anyway, there's no point writing non-optimal asm by hand, IMO, and giving bad example idioms in answers is a no-no. Everything matters, otherwise just use a compiler. (Of course, optimizing for size is valid.) The only time I will relax my stance on efficiency is that writing a boot sector or something is easier in asm, and just has to work. – Peter Cordes Mar 21 '16 at 10:41
  • The way that you've edited your answer is precisely what I meant by _correcting it_. Upvoted! – Fifoernik Mar 24 '16 at 11:26