Help Needed: Understand Assembly Program Processing Direction

Click For Summary

Discussion Overview

The discussion revolves around an Assembly program that reads a string from the keyboard and displays characters that are not upper-case letters in reverse order. Participants explore the implications of not using the Clear Direction Flag (CLD) instruction and address potential issues in the program's logic.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster (OP) questions why their program processes strings in the forward direction without using the CLD instruction.
  • Some participants suggest that DOS automatically sets the CLD before running the program, which may explain the observed behavior.
  • Another participant points out a potential bug in the program related to the CX register not being checked for zero before proceeding, which could lead to unexpected output.
  • There is a discussion about the efficiency of using XOR to clear the CX register compared to moving a zero into it, with some participants defending the use of XOR as a common practice.
  • One participant mentions the importance of handling backspacing for user input, suggesting further improvements to the program.
  • Another participant identifies the code as Intel x86 Assembly based on specific instructions and the use of DOS interrupts.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of the CLD instruction and the handling of the CX register. While some agree on the efficiency of using XOR, there is no consensus on the best practices for error handling in the program.

Contextual Notes

The discussion highlights limitations in the OP's program, including the lack of checks for user input and potential assumptions about the environment in which the program runs. These aspects remain unresolved within the conversation.

Who May Find This Useful

This discussion may be useful for individuals learning Assembly programming, particularly those interested in string manipulation and the nuances of register management in x86 architecture.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 20 ·
Replies
20
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
20
Views
10K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
10K