1

So, I need split a string character by character

in JavaScript it will be like this

let text = "abcde";
for (var i = 0; i < text.lenght; i++);
{
    console.log(text.charAt(i));
}

but how to do this is in NASM?

I am not an expert in assembly, but I have already tried to solve this problem for a week

4
  • So what have you tried? Commented Jun 24, 2022 at 17:38
  • I made a function for quick output and on the calculation of the length of the message I tried to output a character pastebin.com/nEBzNqnu Commented Jun 24, 2022 at 17:46
  • What convention do you use for representing strings? If you don't know the answer to this question, just show us your code you use for defining a string and working with it. Commented Jun 24, 2022 at 20:10
  • @СашенькаГлинин you should add that into the answer! (As code, not as a link) Commented Jun 24, 2022 at 20:25

1 Answer 1

4

I would not precisely call it 'splitting' a string, but rather iterating over a string.

In the data section

Define your string and make it zero-terminated:

MyText db 'abcde', 0

In the code section

Establish a pointer to the beginning of the string:

    mov  esi, MyText

Read a byte from memory:

Again:
    mov  al, [esi]

Test to see if it is the terminating zero:

    test al, al

Stop the loop if it is indeed the end-of-string marker:

    jz   Done

Output the byte in AL any way you like:

    ...

Increment the pointer:

    inc  esi

Go repeat reading a byte:

    jmp  Again
Done:
Sign up to request clarification or add additional context in comments.

3 Comments

Nitpick: the Javascript original that the OP cites enumerates over UTF-16 halfwords (or UCS-2 characters), not over bytes. That said, the OP seems like they are at the level where they don't care.
I get a Segmentation fault on the output, most likely it's the output that I'm doing, sorry for my stupidity, but could you send me the full code?
At the ellipsis, you could write: mov edx, 1 mov ecx, esi mov ebx, 1 mov eax, 4 int 0x80 to output the current character from the string on the screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.