Whats wrong with general purpose register

Started by Khurram, October 08, 2006, 06:35:48 PM

Previous topic - Next topic

Khurram

Thanx Bryant Keller  :U nice explanation now i got clear thoroughly about ah register but its written in my course book about AX register.

AX (accumulator) AX is called the accumulator register because it is favored by CPU for arithmetic operations.

Other operations are also slightly more effecient when performed using AX.


As you quoted

INT 21h contains a table of functions for interfacing with MS-DOS. It uses the AH register to specify which entry in this table to call.like ah,2 (for output function) then for calling this function we call INT21h it means int21h uses
AX (ah) register.


But as my book says
"it is favored by CPU for arithmetic operations"

it doenst match yours quote,though yours quote is too concrete and logical which is clearing me a lot logically.

So the next quote of this para which i extracted from my book

"Other operations are also slightly more effecient when performed using AX."

So is that mean of next para which you deliverd me in my heart and brain??

INT 21h contains a table of functions for interfacing with MS-DOS. It uses the AH register to specify which entry in this table to call.like ah,2 (for output function) then for calling this function we call INT21h it means int21h uses
AX (ah) register.



Thanx a lot

Khurram

Synfire

What your book is talking about is the opcodes themselves. Whereas we are talking about the INT 21h service. When it says that AX is favored by the CPU for arithmetic, it says so because instructions like div use ax explicitly without it being passed as a parameter. That is two different things, the book is right though. What I was talking about was for the DOS services and not the CPU. DOS services use AH for an index like I mentioned above.

MichaelW

Khurram,

There is also an on-line HTML version of Ralf Brown's Interrupt list, here:

http://www.ctyme.com/rbrown.htm

eschew obfuscation

Khurram

Thanx for all of you guys raymond,sinsi,draakie,shantu gadgil,Bryant Keller and MichealW ,At that time yours i was not getting yours response but now when i read yours response again alls view looks alike to me now i am getting nicley.

Now i have two question please take out some time and gimme answer of these two question


Q-1)I have written a small program which takes one character input and i want
to print this input at console i am pasting the code here and giving my
understanding in front of all commands please read this my undrstanding in
front of line and correct me if i am wrong ,if i am not wrong anywhere then tell
me i am getting nicely.

Q-2)Now other problem is this program take me 1 character input but print it
adjacnet to this input character while i wana print it in next line i mean how
can i pass carriage return to get the output at second not adjacent to input
character.


.model small
.stack 100h
.data
.code
  main proc
    mov ah,1  ;<------service no 1 for taking one input character
    int 21h     ;<------Int 21h function calling the function from ah register to take one input character and move this input character into al register
    mov ah,2 ;<----service no 2 for printing input character which is in ah
    mov dl,al  ;<----moving al contents into dl register
    int 21h    ;<----Int 21h function calling the function from ah register to print output from dl register
   mov ah,4ch ;<--service 4ch to move exit from dos shell or ???
    int 21h;<----calling 4ch service from ah register
  main endp
end main


Thanx all of you guys you alls are my teacher :)

I am waiting for yours response.

Khurram

MichaelW

#19
Q1:

Everything looks OK to me except:

service 4ch to move exit from dos shell or ???

This description is from the Microsoft MS-DOS Programmer's Reference:

"End Program (Function 4Ch) terminates the current program and returns control to its parent program."

The parent program is normally, but not necessarily, command.com.

Q2:

The Display Character function (ah=2), like the other DOS display functions AFAIK, calls the BIOS Write Teletype function (Interrupt 10h, ah=0Eh), so it can correctly handle the carriage return, linefeed, and backspace characters. You will need to send both a carriage return and a linefeed, and you will need to preserve the value in AL around the calls (otherwise the value will be overwritten).
eschew obfuscation

Khurram

MichaelW would you please paste the code here..

I tried

.model small
.stack 100h
.data
.code
main proc
  mov ah,1
  int 21h

  mov ah,0Eh
  mov dl,ah
  int 21h

  mov ah,2
  mov dl,al
  int 21h

  mov ah,4ch
  int 21h
main endp
end main


When i run this program it take me input ad then print a sympol (->) right adjacent to this input
character.

While though i moved the E0h in ah register it should print -> after taking input then print input
character as an output character for which i used ah,2.


I mean it seems me my above code just run after taking input till

  mov ah,0Eh
  mov dl,ah
  int 21h

after that it skips

  mov ah,2
  mov dl,al
  int 21h

and goto

  mov ah,4ch
  int 21h


Khurram

sinsi

DOS function 01 is "Read character from standard input, with echo"
  mov ah,1
  int 21h

DOS function 0eh is "Select default drive" and needs the new drive number in DL - MichaelW was referring to BIOS function 0eh "Write teletype" which is INT 10h, so
this code is wrong and should be discarded
  mov ah,0Eh
  mov dl,ah
  int 21h

DOS function 02 is "Write character to standard output" (usually the screen) and needs the character in DL
  mov ah,2
  mov dl,al
  int 21h

And finally DOS function 4ch is "End program" where you can use AL to return a code to the calling program (e.g. ERRORLEVEL in a .bat file)
  mov ah,4ch
  int 21h


So to put it all together, try this

  mov ah,1  ;read a char from the keyboard
  int 21h

  push ax    ;save the char just read

  mov ah,2  ;write a char to the screen
  mov dl,0dh ;this is the CR (carriage return) char
  int 21h
  mov ah,2  ;write a char to the screen
  mov dl,0ah ;this is the LF (line feed) char
  int 21h

  pop ax      ;get the char from the keyboard we saved earlier

  mov ah,2  ;write a char to the screen
  mov dl,al   ;this is the char from the keyboard
  int 21h

  mov ah,4ch
  int 21h


The BIOS has its own set of functions, just like DOS, except they are accesed (for video) via INT 10h.
You really need some sort of reference for DOS functions, since there are around 100 of them, all using a mixture of registers to pass information.
Light travels faster than sound, that's why some people seem bright until you hear them.

Khurram

Thanx a lot for yours reply sinsi you said

You really need some sort of reference for DOS functions, since there are
around 100 of them, all using a mixture of registers to pass information.


Agreed but i dont have such source would you please give me any elementry
level link even though i will bother to you people cause i m dump :).

One more question to all of you guys

service no 1 for taking one input character and move this input character into
AL register.

    mov  ah,1 
    int  21h   


The push instruction doesnt change the contents of AX instead,it copies the
contents of AX onto stack my question is from where AX has contents while
above instruction move input character into AL register (Lower
register of AX) so does it mean push AX instruction copies the contents of AX
(either it is AL or AH) onto stack and why cant we push lower or upper
register of AX


    push ax   



What is stack is it register in assembly Or atack is a data structure which store the data in RAM with LIFO discipline??

Thanx again sinsi

Khurram

Shantanu Gadgil

Hi Khurram,
All I can say is you are getting _yourself_ tangled in too many things all at once. Its good that you want to learn all the stuff but if you try to do too much, you will (probably) not like the whole experience!!!
Questions like "what is a stack", etc though NOT related ONLY to assembly, are a general DATA-STRUCTURES concept.

How stacks would work (and how you would work with stacks) in assembly, would be found in an ASM reference book.

That out of the way...I would say the following (as already told by sinsi) and (would like to add that you should consider these things a MUST-HAVE :bg :bg while learning).
One can learn _only so much_ from a medium like the internet, but the text-books would definitely help you more.
This reference material would also contain sample source codes, which would HELP you, and not make you feel kinda lost!  :bg :bg

(I don't want to get into a "text-books better or online resources better" debate here, just mentioning to Khurram that having a DOS reference in text-book format would be a right thing to have)

So, for DOS, start with some kind of manual like the book by Ray Duncan. When (IF) you feel you need to do more than that, then there is the Ralf-Brown list.

Again, I would like to say...do get some kind of TEXT-BOOK as a reference guide!!!

Cheers and Happy Programming,
Shantanu
To ret is human, to jmp divine!

sinsi

OK. Go here and download Part A, Part B, Part C and Part D.
Then download RBILVIEW and you will have Ralf Brown's Interrupt List on your computer, with a nice easy viewer.
Or, as MichaelW pointed out, it is available online.

As Shantanu said, get a book. I have "PC Interrupts, 2nd edition" which is essential for all kinds of real mode stuff (ISBN 0-201-62485-0).
Light travels faster than sound, that's why some people seem bright until you hear them.

Khurram

hi

Guys you are right i need some reference book but in this book there is no
sample code or example , what MichealW gave me link its interrupt list which
tells how to do and what to do but without example which is comlicated for
me cause i am newbie.I have serached in my refernce book about showing the
string at console but only get the interrupt ah,9 for ouput the string.I tried
but no luck i hope you guys will help me.

.model small
.stack  100h
.data
string db 'Khurram:$'
.code
  main proc
   mov ah,9
   mov dx,offset string
   int 21h
   mov ah,4ch
   int 21h
  main endp
end main


above code compiles but giving some machine code as an ouput why.

Khurram



MichaelW

If you look in the Interrupt List at the entry for Interrupt 21h Function 09h, you will see that the function expects the string to be at DS:DX. In other words, the string must start at the offset address specified by DX, in the segment specified by DS. The problem here is that DS is not set to your data segment. For an EXE program, the program itself must set DS to the data segment. The easiest method of doing this is to place a .STARTUP directive at the entry point of the program.

.model small
.stack  100h
.data
string db 'Khurram:$'
.code
.startup
  main proc
   mov ah,9
   mov dx,offset string
   int 21h
   mov ah,4ch
   int 21h
  main endp
end

Another method is to load the value of the value of the predefined symbol @data into DS.

.model small
.stack  100h
.data
string db 'Khurram:$'
.code
  main proc
   mov ax, @data
   mov ds, ax
   mov ah,9
   mov dx,offset string
   int 21h
   mov ah,4ch
   int 21h
  main endp
end main
eschew obfuscation

Khurram

Michealw thanx a lot Dear ,i got yours second example but one thing is hitting
me why first we move data segment (@data) into AX register why not
data segment (@data) into DS register.

Is my understanding  correct

data segment is [string db 'Khurram:$']??

and this data segment is referred by @data??

Please dear dont irritate just bear me only for some days then i would be picky
by reading manuals.

Khurram

Synfire

He's moving @data into AX then into DS. You can't directly move @data into DS so you must use a register. This is one of the things the .startup directive does for you. There are certain things you can't move a memory location into, segment registers and other memory locations come to mind. The most common way to achieve this is to move it into a register first 'mov ax, @data' then move the value in the register to the segment register 'mov ds, ax'. You could also probably push/pop the value, but I don't like that method since it's slower.

Khurram

synfire copuple of confusion please help

-what is @data is it directive which contains data segment memory adress or what is it?

-Is there any reason behind that why we cant move memory location to segment register?

-i have read at several places that first move into AX,then into DS but nowhere written the reason for that we cant move memory register directly to data segment?

-Why only first move into AX there is other register like BX,CX,DX ?

Khurram