Author Topic: accepting array of strings  (Read 11737 times)

Offline tejaswi2195

  • Jr. Member
  • *
  • Posts: 9
accepting array of strings
« on: January 09, 2014, 04:16:52 PM »
code---->
Code: [Select]
SECTION .bss
array: resq 10
result: resb 16
count: resb 3
num: resb 9

SECTION .data
msg: db "enter string",10
len: equ $-msg

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro


SECTION .text
global _start
_start:

mov rcx,array
mov byte[count],02h

l1:
dec byte[count]
write msg,len
read rcx,9
add rcx,8
cmp byte[count],00h
jne l1

mov rbx,array
call convascii
write result,16

mov rax,60
mov rdi,0
syscall

convascii:
mov rsi,result
mov rcx,10h

l3:
rol rbx,04
mov al,bl
and al,0Fh
cmp al,09h
jbe add30
add al,07h

add30:
add al,30h
mov [rsi],al
inc rsi
loop l3
ret


this program is meant to take two strings from user and store it in variable array
but it accepts one string and terminates

and if i need to move eles in array one by one to other location how to do it?
« Last Edit: January 09, 2014, 05:09:24 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: accepting array of strings
« Reply #1 on: January 09, 2014, 05:32:46 PM »
First problem: syscall trashes rcx (accursed 64-bit marketing hype!). More later.

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: accepting array of strings
« Reply #2 on: January 09, 2014, 09:30:07 PM »
Okay, another problem... you're putting the address of "array" in ebx and converting it to hex ascii. Is that what you want to do? I changed a couple of things...
Code: [Select]
SECTION .bss
array: resq 10
result: resb 16
count: resb 3
num: resb 9

SECTION .data
msg: db "enter string",10
len: equ $-msg

%macro read 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro

%macro write 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro


SECTION .text
global _start
_start:

mov rcx,array ; syscall trashes ecx - I changed it to ebx
mov byte[count],02h

l1:
dec byte[count]
write msg,len
read rcx,9 ; and here
add rcx,8 ; and here.
cmp byte[count],00h
jne l1

mov rbx,array ; changed to [array]
call convascii
write result,16

mov rax,60
mov rdi,0
syscall

convascii:
mov rsi,result
mov rcx,10h

l3:
rol rbx,04
mov al,bl
and al,0Fh
cmp al,09h
jbe add30
add al,07h

add30:
add al,30h
mov [rsi],al
inc rsi
loop l3
ret

At this point, it reads two strings and displays the ascii codes of the first string (only) in reverse order. That is, if the pesky user (me) types "1234"(enter), it displays "00000A34333231". Maybe you wanted address of array after all...

If the pesky user types more characters than are allowed in rdx, the "excess" remains in the "keyboard buffer" to be read on the next sys_read or ends up on the command prompt, as expected. You might want to flush the buffer to make it a little more "user-proof", but it's okay for a well-behaved user.

You ask about moving strings from one place to another...
Code: [Select]
mov rsi, source_buffer ; address
mov rdi, dest_buffer ;address
mov rcx, length_to_move (might want "[contents]" here)
rep movsb

But it might suit your purposes better to swap pointers to the strings rather than moving the bytes around. I'm not entirely sure what you're trying to do.

Best,
Frank


Offline tejaswi2195

  • Jr. Member
  • *
  • Posts: 9
Re: accepting array of strings
« Reply #3 on: January 10, 2014, 12:33:01 AM »
actually this prog reads 2 strings and prints address of array o.e why mov rbx,array and then conversion
and if i need to move array to otherlocation without using string inst can i use

mov rbx,[souce array]
mov [destn array],rbx

and then increment souce array and destn array by 8 bytes and copy next element of array??

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: accepting array of strings
« Reply #4 on: January 10, 2014, 12:51:52 AM »
Yeah, I guess you should be able to do that.

Best,
Frank