Need help undertstanding assembly code

Can someone please explain to me this code line by line (or as detailed as possible) please?

[code]
.model small
.stack 100h

.data

buffer db 100 dup(0)
entry db "Welcome to simple edit program",0ah,0ah,'$'
enter db "Please enter character - ESC to leave",0ah,0dh,'$'
endit db "Terminate the program",0ah,0dh,'$'

.code

; A marco for printing messages..

mdisplay macro string
push ax ;push ax onto the stack
push dx ;same for dx
mov ah,9 ;service 9 (drint a string).
mov dx,offset string ;the string to print
int 21h ;do it!
pop dx ;pop dx off stack
pop ax ;pop ax off stack
endm
; to end the marco..



main proc near
mov ax,@data
mov ds,ax
mdisplay entry
;-----------------------------------------
mov bx,offset buffer
mdisplay enter
.repeat
next: call readkey ;read a key value
call dispch ;echo to screen
mov byte ptr[bx],al ;store in buffer
inc bx ; increment buffer pointer
.until (bx == offset buffer+100 || [bx-1]==1bh)

mdisplay endit
call dispbuff
;----------------------------------------
mov ax,4c00h
int 21h
;ret
main endp

;------------------------
readkey proc near
mov ah,0
int 16h
ret
readkey endp
;------------------------
dispch proc near
push bx
mov bx,0
mov ah,14
int 10h
pop bx
ret
dispch endp
;------------------------
dispbuff proc near
mov bx,offset buffer
.repeat
mov al, byte ptr [bx]
call dispch
inc bx
.until (bx == offset buffer + 100)
ret
dispbuff endp
end[/code]

Comments

  • 1) .model small - says that the program is going to be small and so does not need lots of memory.

    2) .stack 100h - says define a stack with 256 bytes

    3) .data - says define in data segment. What follows is data and not executable code.

    4) buffer db 100 dup(0) - says define in memory 100 bytes and initialize each byte to a value of zero and name it buffer

    5) entry db "Welcome to simple edit program",0ah,0ah,'$' - says define in memory as many bytes as needed (dictated by the length of the data string) and initialize each byte to a value from the string in order. and name it entry. Thus in this case a memory location that is accessible by the name entry will have 33 bytes containing the english phrase followed by a line feed, and again followed by a line feed and then the charcter $. A hex value of 0a is line feed on the output device. The $ ends a string like a null character in C lanuage.

    6) enter db "Please enter character - ESC to leave",0ah,0dh,'$' - says define in memory as many bytes as needed (dictated by the length of the data string) and initialize each byte to a value from the string in order. and name it enter. Thus in this case a memory location that is accessible by the name enter will have 40 bytes containing the english phrase followed by a line feed, and then followed by a carriage return and then the charcter $. A hex value of 0d is carriage return on the output device.

    7) endit db "Terminate the program",0ah,0dh,'$' - says define in memory as many bytes as needed (dictated by the length of the data string) and initialize each byte to a value from the string in order. and name it endit. Thus in this case a memory location that is accessible by the name endit will have 24 bytes containing the english phrase followed by a line feed, and then followed by a carriage return and then the charcter $.

    8) .code - says define in code segment. What follows is executable code.

    9) ; A marco for printing messages.. is a comment line in Assembler (since it starts with a ;) and the assembler does not process it. It is for documenting the code. Note typo error, not marco but macro

    10) mdisplay macro string - says that you are starting to define a macro named mdisplay which will take a string as a parameter

    11) push ax ;push ax onto the stack - says save the contents of the ax register on the stack. This suggests that the program is going to use the ax register and so as to not clobber what was in it, we save it on the stack. Later on we will restore ax register as a good responsible programming citizen. Note the inline comment, again not processed by the Assembler, just for documentation.

    12) push dx ;same for dx - says save the contents of the dx register on the stack. This suggests that the program is going to use the dx register and so as to not clobber what was in it, we save it on the stack. Later on we will restore dx register as a good responsible programming citizen. Note the inline comment, again not processed by the Assembler, just for documentation.

    13) mov ah,9 ;service 9 (drint a string). - says get the value 9 into the ah register. Note that the ax register is made up of the ah and al registers. ah and al are byte regsiters and ax is a 16 bit register. There is a typo. (drint a string) should read (print a string).

    ----------------
    | ah | al |
    ----------------

    ah and al together is the 16 bit ax register

    14) mov dx,offset string ;the string to print - says get the offset of the passed string into the dx register. Again dx register is made up of the dh and dl registers. dh and dl are byte regsiters and dx is a 16 bit register. The way the actual address of the string is calculated is taking the contents of the ds (data segment) register and adding to it the contents of the dx register. There is a shift operation that is done in the ds regsiter but never mind that. Suffice to say that the address to the string is gotten from adding the contents of the ds and dx registers.

    15) int 21h ;do it! - says call the DOS interrupt function 21h which is decimal 33. The way DOS was designed is that it provided a number of functions to do mundane things. In our example, if you load the ah register with 9, the dx register with the offset into the data segment (as pointed to by the ds regsiter) of a valid string and then issue the instruction 'int 21h' DOS will try to send the string to the default output device. There are many DOS 'int' calls and each one takes specfied values in specified regsiters that need to be loaded before the DOS 'int' call is made. Read the DOS book.

    16) pop dx ;pop dx off stack - says restore the dx regsiter from the stack.

    17) pop ax ;pop ax off stack - says restore the ax regsiter from the stack.

    18) Note we pop from the stack in the reverse order we push on the stack. This is because stack employs the LIFO (Last In First Out) paradigm.

    19) endm - says end the macro definition

    20) ; to end the marco.. - is a comment line

    Now the Main Program.

    21) main proc near - says this is the main program, the near says it is a small model program (has addressing implications)

    22) mov ax,@data - says get the offset of the 'data' into the data segment into the ax register. Note that our data is at the very beginning of the data segment. Now load the ds register with contents of ax register. There are rules as to how certain registers may be loaded

    23) mdisplay entry - calls the macro defined above passing it the address of entry so "Welcome to a simple edit program" followed by 2 line feeds is sent to the output device

    24) mov bx,offset buffer - loads the bx register with the offset of location buffer into the data segment

    25) mdisplay enter - calls the macro defined above passing it the address of enter so "Please enter character - ESC to leave" folllowed by a line feed and a carriage return is sent to the output device

    26) .repeat - is for looping control

    27) next: call readkey ;read a key value - is the looping point. It calls readkey

    28) readkey is a near proc which sets the protocol to issue a function call of 0 on DOS int 16h (decimal 22) which returns the key pressed by the user in the al register

    29) call dispch ;echo to screen - says to call dispch

    30) dispch is a near proc which sets the protocol to issue a function call of hex e (decimal 14) on DOS int 10h (decimal 16) which displays the character in the al register

    31) mov byte ptr[bx],al ;store in buffer - says to save the contents of the al regsiter (the key pressed by the user) into the buffer.

    32) inc bx ; increment buffer pointer - says to increment the pointer into buffer (by incrementing bx register) to be ready for storing the next character to be read

    33) .until (bx == offset buffer+100 || [bx-1]==1bh) - says continue reading characters until either the buffer if full (100 characters) or the ESC character (hex 1b - decimal 27) was the last character read

    34) mdisplay endit - says when the condition in (33) above is satisfied call the near proc dispbuff. By now it should be obvious that the environment is is to repeatedly call dispch to display the contents of the buffer a character at a time on the output device for 100 characters.

    35) Note each proc is ended with the endp










Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion