* programming bug
@ 2004-01-16 13:21 ergün koray
2004-01-16 14:44 ` John Rodriguez
0 siblings, 1 reply; 2+ messages in thread
From: ergün koray @ 2004-01-16 13:21 UTC (permalink / raw)
To: linux-assembly
hi there,
i am new to assembly and i have written just this tiny code that turns
lowercase characters to uppercase.
but unfortunately after the first characters have been converted the
program exits, and i can't find why ?
can anybody help me ? thanks
.data
chr: .byte
.text
.global _start
_start:
read:
movl $3, %eax /* read */
movl $1, %ebx /* from stdin */
movl $chr, %ecx /* to chr */
movl $1, %edx /* 1 char */
int $0x80 /* using int 0x80 */
xorl %eax, %eax /*eax = 0 */
xorl %ebx, %ebx /*eax = 0 */
xorl %edx, %edx
xorl %ecx, %ecx
check:
movb chr, %al
cmpb $'a', %al
jl endprog
cmpb $'z', %al
jg endprog
xorl %eax,%eax
convert:
movb chr, %al /* al <- chr */
subb $'a' , %al /* Convert small letter */
addb $'A', %al /* to capital */
movb %al, chr /* write converted char to memory */
write:
movl $4, %eax /* write */
movl $1, %ebx /* to screen */
movl $chr, %ecx /* from chr */
movl $1, %edx /* 1 char */
int $0x80 /* using int 0x80 */
xorl %eax, %eax
xorl %ebx, %ebx
xorl %ecx, %ecx
jmp read
endprog:
movl $1, %eax /* exit sys call */
movl $0, %ebx /* exit value */
int $0x80 /* call int */
^ permalink raw reply [flat|nested] 2+ messages in thread
* RE: programming bug
2004-01-16 13:21 programming bug ergün koray
@ 2004-01-16 14:44 ` John Rodriguez
0 siblings, 0 replies; 2+ messages in thread
From: John Rodriguez @ 2004-01-16 14:44 UTC (permalink / raw)
To: 'ergün koray', linux-assembly
There are a couple of bugs here.
First, although it worked for some reason, stdin is 0 while stdout is 1
Perhaps the assembler catched the error? Who knows. Just make a note.
:)
So your code as written says,
"check character to see if is lower case letter"
"if not, exit"
"otherwise, convert to upper and continue"
The problem with this is:
a) what if I put "The", it should return "THE" ignoring the first
character.
However, your code will see "T", complain, and exit
b) if I put "many words", your code will see "m","a","n","y" and do as
expected, but when it sees " " (the space between the words -- counts as
a valid character), it will complain and exit.
The solution is to add more error checks as the following.
ASCII goes roughly like this
0 NUL
..
13 newline
..
65 A
..
90 Z
..
96 a
..
122 z
..
So
"if char is newline", stop, end of input from keyboard
"if less than a", ignore (punctuation, upper case letter, etc)
"if less or equal to z", then is lower case letter, CONVERT this
"else", it's some other meaningless character, just ignore
The order is important since the second test will only be reached if the
first test failed (that is, it's not a newline).
Hope this helps.
One last note, xorl'ing each register per step doesn't seem "proper" for
two reasons.
1) Some of the registers aren't used at all, and so can be reused.
For example %edx which tells the number of characters to read/write is
always 1. So basically you clear it and set it to 1, over and over...
2) If this is extended as a function later on, you should "save" the
registers so that the caller gets back the same state that it left when
it called the function, just an FYI.
-John
-----Original Message-----
From: linux-assembly-owner@vger.kernel.org
[mailto:linux-assembly-owner@vger.kernel.org] On Behalf Of ergün koray
Sent: Friday, January 16, 2004 8:21 AM
To: linux-assembly@vger.kernel.org
Subject: programming bug
hi there,
i am new to assembly and i have written just this tiny code that turns
lowercase characters to uppercase.
but unfortunately after the first characters have been converted the
program exits, and i can't find why ?
can anybody help me ? thanks
.data
chr: .byte
.text
.global _start
_start:
read:
movl $3, %eax /* read */
movl $1, %ebx /* from stdin */
movl $chr, %ecx /* to chr */
movl $1, %edx /* 1 char */
int $0x80 /* using int 0x80 */
xorl %eax, %eax /*eax = 0 */
xorl %ebx, %ebx /*eax = 0 */
xorl %edx, %edx
xorl %ecx, %ecx
check:
movb chr, %al
cmpb $'a', %al
jl endprog
cmpb $'z', %al
jg endprog
xorl %eax,%eax
convert:
movb chr, %al /* al <- chr */
subb $'a' , %al /* Convert small letter */
addb $'A', %al /* to capital */
movb %al, chr /* write converted char to memory */
write:
movl $4, %eax /* write */
movl $1, %ebx /* to screen */
movl $chr, %ecx /* from chr */
movl $1, %edx /* 1 char */
int $0x80 /* using int 0x80 */
xorl %eax, %eax
xorl %ebx, %ebx
xorl %ecx, %ecx
jmp read
endprog:
movl $1, %eax /* exit sys call */
movl $0, %ebx /* exit value */
int $0x80 /* call int */
-
To unsubscribe from this list: send the line "unsubscribe
linux-assembly" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
-
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-01-16 14:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-16 13:21 programming bug ergün koray
2004-01-16 14:44 ` John Rodriguez
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox