Help Needed: Understand Assembly Program Processing Direction

AI Thread Summary
The discussion centers on an Assembly program that reads a string from the keyboard and displays non-uppercase characters in reverse order. The original poster questions why the program runs correctly without using the Clear Direction Flag (CLD) instruction, noting that DOS initializes this flag before execution. Participants highlight a potential bug in the program related to not checking if the CX register is zero, which could lead to outputting garbage characters. Suggestions for improvement include handling backspacing for user input and providing additional resources for better understanding. Overall, the conversation emphasizes the importance of proper register management and error handling in Assembly programming.
duynguyenvan
Messages
4
Reaction score
0
I need help, pls !

Hello,
Could you pls help me answer this question:
- Please take a look at my small Assembly program that reads a tring from keyboard, then displays all the characters that are not uper-case letters in reverse order:

.model small
.stack 100
.data
tb1 db 'Pls enter a string, enter to finish $'
tb2 db 10,13,'The characters that are not uper-case : $'
xau db 80 dup(?)
.code
Main proc
mov ax,@data
mov ds, ax
mov es, ax
mov ah,9
lea dx,tb1
int 21h
xor cx,cx
lea di,xau
nhap: mov ah,1
int 21h
cmp al,13
je thoi
cmp al,'A'
jb dung
cmp al,'Z'
ja dung
jmp nhap
dung: stosb
inc cx
jmp nhap
thoi: mov ah,9
lea dx,tb2
int 21h
dec di
mov si,di
std
mov ah,2
quay: lodsb
mov dl,al
int 21h
loop quay
mov ah,4ch
int 21h
Main endp
End main

The code above runs very well, even though i did not use the Clear Direction Flag (cld) instruction after the instruction in red (lea di,xau)
The string processing is still in forward direction.
The question is that: Why?

I appreciate your help.

Regards,
Duy.
 
Last edited:
Physics news on Phys.org
Don't multiple post this especially not as a reply on other peoples threads.
And you aren't doing IBM any favours by advertising yourself as the project leader.
 
mgb_phys said:
Don't multiple post this especially not as a reply on other peoples threads.
And you aren't doing IBM any favours by advertising yourself as the project leader.

I deleted the cross-posts in other people's threads. That was a lot of work.

duynguyenvan, welcome to the PF, but please do not cross-post. You should get the help you need here in this thread. Thank you.
 
mgb_phys said:
Don't multiple post this especially not as a reply on other peoples threads.
And you aren't doing IBM any favours by advertising yourself as the project leader.

Dear all,
I'm so sorry for that, i just copied and pasted from my Email (contains the signature) directly into the post ...
 
duynguyenvan said:
Dear all,
I'm so sorry for that, i just copied and pasted from my Email (contains the signature) directly into the post ...

Okay, no worries. The work's been done to clean it all up, and hopefully you will get some good help on your question here at the PF.
 
Upon further review, I suspect a problem. OP, what do you mean by this:

xor cx,cx

That wouldn't seem to do anything.
 
Last edited:
what do you mean? that is to clean up the cx register, and use the register for counting the number of the characters that are not upper-case letters later.
By the way, i have not had much experience of doing with Assemply stuffs, that's why i asked you guys the "simple" question ... can you understand and answer my question, berkeman?
pls mail me to this address: duyngv@us.ibm.com
if you have time to help me understand about my question, your help will be really appreciated !
 
Quiz question -- what would be an easier way to clean up the cx register?
 
berkeman: unfortunately the OP is correct. That is a much used way to clear a register, rather than moving a zero in. On some architectures the move zero is actually slower, as the xor doesn't need to touch anything apart from one register (no extra circuitry is used).
 
  • #10
genneth said:
berkeman: unfortunately the OP is correct. That is a much used way to clear a register, rather than moving a zero in. On some architectures the move zero is actually slower, as the xor doesn't need to touch anything apart from one register (no extra circuitry is used).

Thanks for the feedback, genneth. I guess I haven't worked with that variation. I'll go back and clean up some of my comments, and see if I can offer some help on their question...
 
  • #11
I've got the answer, thanks!

>Could you pls help me answer this question:
>- Please take a look at my small Assembly program that reads a tring from
>keyboard, then displays all the characters that are not uper-case letters in
>reverse order:
>
>The code above runs very well, even though i did not use the Clear Direction
>Flag (cld) instruction after the instruction in red (lea di,xau)
>The string processing is still in forward direction.
>The question is that: Why?

DOS sets up the CLD for you, before starting the program. It's an
assumption you can make, if you want. I often set or clear it anyway,
just to be sure. But DOS does prepare a few things before running the
program.

Also, your program has a bug. It does NOT check to see if CX is zero
(nothing was entered) before proceeding. You instead use a LOOP
instruction at the end, which if CX starts as zero will output 65535
garbage characters on the screen. This obviously isn't good practice.

You might consider also dealing with backspacing, in case the user
wants to edit their line.

I'm including two files in return that do these things. The longer
file includes a general template for both .EXE and .COM files. You
might want to read through it, though perhaps it is too much to wade
through. The other one attempts to be a fairly close representation
of what you already wrote, doesn't include much comment, and just gets
the job done.

Jon
 
  • #12
Unrelated, but... how do you know what type of microprocessor code this is?

We learned one in a family from Motorola, but I wouldn't be able to tell off the top of my head what each instruction does, aside from the basic MOV, xor, ...
 
  • #13
ax,dx,al etc? Definitely Intel x86. And int 21h seals it as DOS.
 
Back
Top