Author Topic: First character of string  (Read 7913 times)

Offline mk8

  • Jr. Member
  • *
  • Posts: 5
First character of string
« on: May 01, 2014, 06:58:55 PM »
Hi, this post might be very poor, but I cant find it anywhere so I decided to ask here, if this question is really bad, just deleted this topic. I just started with nasm and I am able to print string what I need in this way
Code: [Select]
section .bss
section .data

  string: db "some string",0
  string_l: equ $-string

section .text

global _start

_start:

mov eax, 4
mov ebx, 1
mov ecx, string
mov edx, string_l
int 80h

mov eax, 1
mov ebx, 0
int 80h

But I am try to display only first character, I tried [string+1].
And what if I wanted to display N element of string?

Thanks a lot!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: First character of string
« Reply #1 on: May 01, 2014, 08:37:58 PM »
sys_write will print the number of characters in edx, so if you want just one character, tell edx 1.

"[string + 1]" would refer to the second byte of "string", but ecx wants the address, not "[contents]"...
Code: [Select]
lea ecx, [string + 1]
would do it.

If you want it in a register...
Code: [Select]
section .bss
section .data

  string: db "some string",0
  string_l: equ $-string

  N dd 6 ; might use a more meaningful name,,,

section .text

global _start

_start:

mov esi, [N] ; or get it some other way

mov eax, 4 ; sys_write
mov ebx, 1 ; sdtout
lea ecx, [string + esi]
mov edx, 1 ; just one, please
int 80h

mov eax, 1 ; sys_exit
mov ebx, 0 ; claim "no error"
int 80h
(untested)

The "stupid" question is one you need to know the answer to but don't ask. :)

Best,
Frank


Offline mk8

  • Jr. Member
  • *
  • Posts: 5
Re: First character of string
« Reply #2 on: May 01, 2014, 09:17:59 PM »
Thank you very much for this reply!
I get it now, once again thank you ;)