1 / 2
Feb 2005

section .bss
        buffer          resb    1
section .text
        global _start
_start:
                call ReadChar
                cmp al, '4'
                jne wait_enter
                ; we've got '4'
                call ReadChar
                cmp al, '2'
                jne not_42
                ; we've got '42'
                mov eax, 1
                xor ebx, ebx
                int 80h
not_42:         ; we've got '4x'
                mov dl, al
                mov al, '4'
                call PrintChar
                mov al, dl
                jmp wait_middle
wait_enter:     ; nothing there, waiting for '\n'
                call PrintChar
wait_loop:
                call ReadChar
wait_middle:
                call PrintChar
                cmp al, 10
                je _start
                jmp wait_loop
;------- ReadChar --------------------
; reads one character from stdin into AL
ReadChar:
                push edx
                mov eax, 3
            xor ebx, ebx
            mov ecx, buffer
            mov edx, 1
            int 80h
            mov al, [ecx]

            pop edx
            ret
;------ PrintChar ------------------
; prints character from AL to stdout
PrintChar:
                push eax
                push edx
                mov [buffer], al
            mov eax, 4
            mov ebx, 1
            mov ecx, buffer
            mov edx, ebx
            int 80h

            pop edx
            pop eax
            ret
  • created

    Feb '05
  • last reply

    Jun '05
  • 1

    reply

  • 1.5k

    views

  • 2

    users

4 months later

see my code for test

	global _start
section .data
	buffer	dw	0h
section .text
_start:
	mov		ecx, buffer
	mov		edx, 02h
	call		read
	mov		cx, word [buffer]
	cmp		cx, 3234h
	je		exit
	cmp		ch, 0ah
	je		one_dig
	jmp		two_dig
one_dig:
	mov		ecx, buffer
	mov		edx, 02h
	call		write
	jmp		_start
two_dig:
	mov		ecx, buffer
	mov		edx, 02h
	call		write
	mov		edx, 01h
	mov		ecx, buffer
	call		read			; read the 0ah
	mov		ecx, buffer
	call		write			; write the 0ah
	jmp		_start
exit:
	mov		eax, 01h		; exit()
	xor		ebx, ebx		; errno
	int		80h
read:
	mov		eax, 03h		; read()
	mov		ebx, 00h		; stdin
	int		80h
	ret
write:
	mov		eax, 04h		; write()
	mov		ebx, 01h		; stdout
	int		80h
	ret