in the name of zero

July 24, 2006

practice

Filed under: hermetic studies

we’re doing 16 bit real mode assembly in class this semester so i’m making all my class work inside an emulator. this time, i’m doing something in 32 bit protected mode linux (as a refresher).

it’s supposed to be a program that waits for user input and tells if it’s a palindrome or not.

%define _NR_read	0x3
%define _NR_write	0x4
	
	global _start
section .text
_start
	mov edx, ebuf		; size_t count
	mov ecx, buf		; void *buf
	xor ebx, ebx		; STDIN
	mov eax, _NR_read	; sys_read()
	int 0x80		; call kernel
	
	; -- strlen() --
	cld			; clear direction flag
	mov edi, buf		; start of buffer
	mov eax, 0		; null terminator
	mov ecx, -1		; counter = 255
repne	scasb			; repeat till NULL terminator
	not ecx			; invert ecx
	lea ecx, [ecx-2]	; ecx = strlen(buf)
	
	; -- setup pointers --
	mov esi, buf		; esi to first letter
	mov edi, buf		; same
	dec ecx
	add edi, ecx		; edi to last letter
	
	mov ebx, 1		; STDOUT
	mov eax, _NR_write	; sys_write()
	
do_check
	mov cl, [esi]
	mov ch, [edi]
	cmp cl, ch		; are they equal?
	jne wrong		; no. word is not a palindrome
	
	; -- work towards the middle
	inc esi
	dec edi
	cmp edi, esi		; are we at the middle?
	jns do_check		; no. repeat check
	
	; -- palindrome! --
	mov edx, (badmsg-gudmsg)
	mov ecx, gudmsg
	int 0x80		; call kernel
	
	jmp exit
	
wrong	; -- not palindrome! --
	mov edx, (endmsg - badmsg)
	mov ecx, badmsg
	int 0x80
	
exit
	xor eax, eax
	xor ebx, ebx
	inc eax
	int 0x80
	
section .data
	gudmsg	db 'string is a palindrome', 0xa, 0x0
	badmsg	db 'string is not a palindrome', 0xa, 0x0
	endmsg
section .bss
	buf	resb 0x32	; 50 byte buffer
	ebuf	equ $-buf

1 Comment »

The URI to TrackBack this entry is: http://gnurbs.blogsome.com/2006/07/24/practice/trackback/

  1. note to self:

    try using the return value of read() next time?

    Comment by sleepy jenkins — July 26, 2006 @ 7:40 am

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Get free blog up and running in minutes with Blogsome | Theme designs available here