0 Replies - 4757 Views - Last Post: 06 December 2009 - 09:20 AM

#1 kcghost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 09

NASM: Simple Linux Console Input

Posted 06 December 2009 - 09:20 AM

This is a dead simple problem, but for some reason I cannot find any example code online, so I managed to get it half working off my own guesses as to how to use system calls.
All I want is a simple string or character input, by which part I actually have working, it just doesn't work again - you can't do it twice in the same program.

I am working in Linux with NASM, so I'm using linux system calls.
Here is my code, largely based off of a modified "Hello World":

section .text
global _start
_start:
	mov	 ecx,hello;second argument: pointer to message to write
	call	write_string
	mov	ecx,charinputa;pointer to data to read into
	call	read_char
	call	write_string
	mov	ecx,newline
	call	write_string
	mov	ecx,charinputb
	call	write_string
	jmp	exit

read_char:	;read in only one character from terminal
	pusha	;save state before call
	pushf
	mov	eax,3;(sys_read)
	mov	ebx,0;(stdin)
	mov	edx,1;length 1
	int	0x80			  ;system call
	popf
	popa
	ret

write_string:
	pusha	;save state before call
	pushf	;find the length of the string
	xor	edx,edx;edx = 0
	mov	al,11111111b
null_test:
	test	[ecx+edx],al;testing for zero flag
	jz	found_length
	inc	edx;edx will be the length, argument needed by kernel call
	jmp	null_test
found_length:
	mov	 ebx,1  ;first argument: file handle (stdout)
	mov	 eax,4  ;system call number (sys_write)
	int	 0x80;call kernel
	popf	;return previous state
	popa
	ret

exit:
	mov	ebx,0;first syscall argument: exit code
	mov	 eax,1  ;system call number (sys_exit)
	int	 0x80;call kernel


section .data
hello	 db	  "Hello, world!",0Dh,0Ah,0;added CR and LF for a new line, terminates with 0
newline	  db	0x0D,0x0A,0	;CR and LF for a new line, terminates with 0

section .bss
charinputa	resb	1	;reserve one byte for one char
charinputb	resb	1



I compile using:
nasm -f test.asm
ld -o test -m elf_i386 test.o
I am using a 64-bit processor, which is why the -m emulation is necessary, I don't believe that is whats giving me problems.

This code will print out "Hello, world!" and a newline as its supposed to. Then you may enter a character, once you press enter it displays that character on a newline, then quits without letting you input another character. I am guessing that the second time its simply taking zeroes as input - causing it to print nothing. I guess there must be some sort of resetting procedure I am not getting. Funny thing, I thought this was working and went on to write up a string input, (testing it after the char input) and got very frustrated until I finally realized I couldn't do two inputs of any sort one after another. Gotta "love" debugging. Thanks in advance for any help.

This post has been edited by kcghost: 06 December 2009 - 09:23 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1