Question

I am trying to create a subroutine that allows the user to load a register with an ASCII buffer and have it print to screen by calling the subroutine.

I am guessing my while loop for incrementing %edi (counter) is not seeing the termination character.

I am also wondering if I am using the proper addressing mode (I am using register indirect for the while loop).

.data

  nts:

    .ascii "This is a string.\0"


.text

  .global _start

  .include "print.asm"

  _start:

    mov $ntstring, %ebx

    call PrintString
    mov $1, %eax



    int $0x80

Here is my include file:

PrintString:
        push  %ebp              
        :      

        cmp $0, (%ebx,%edi) #i am trying to do a while loop that increments edi until
                            #it encounters the null terminator "\0"
        je Print

        add   $1,  %edi         

        jmp StringNotEmpty
Was it helpful?

Solution

As you noticed, the problem is in your check for the NUL terminator:

cmp $0, (%edx,%edi)

On 32-bit, the default operand size is a 32-bit word, therefore you'd need to end the string with 4× \0 in order for it to stop (or be lucky and encounter a zero-filled garbage after your string).

In this case, you want to load and check just one 8-bit character, therefore you need to use the b suffix like this:

cmpb $0, (%edx,%edi)

You can see all the different suffixes here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top