From: Umesh on
(a) Write an assembly language program to accept a decimal number and
display it's two's complement representation in binary and hexadeimal
formats. (5 Marks)
Ans.
Convert . Input type can be H, D or B msdos equ 21h ;
MSDOS irq fwrite equ 40h ;
write to file with handle exit equ 4c00h ;
msdos exit mov ax,cs ;
get code segment mov ds,ax ;
use it now mov si,0080h ;
DOS command line page 0 lodsb ;
load size of command line cmp al,0 ;
anything on command line ? jbe usage ;
noo, show usage cbw ;extend AL to AX xchg bx,ax ;
swap size to bx for indexing mov byte [bx+si],0 ;
null terminate command line call parse ;
parse command line jmp main ;
go on with main usage: mov bx,utext ;
pointer usage text jmp errout ;
skip this main: xor eax,eax ;
wanna clean registers xor ebx,ebx xor ecx,ecx xor edx,edx ;
calculate length of 1:st arg mov si,inbuff ;
get input buffer get0: lodsb ;load AL cmp al,0 ;
end of arg ? je got0 ;yeahh inc cx ;
noo, increase length count jmp get0 ;
check next got0: ;
first arg len in CX now mov si,inbuff ;
get command line dump: lodsb cmp al,0 ;
second command ? jne dump ;noo lodsb and al,0dfh ;
force upper case cmp al,'H' ;
hex input ? je dohex cmp al,'D' ;
decimal ? je dodec cmp al,'B' ;
binary ? jne usage ;
noo, let us know we're wrong ;
check for valid BIN input mov si,inbuff ;
command line buffer mov bx,errt1 ;
error text isbin: lodsb ;load AL cmp al,'0' ;
a zero ? je bindo ;yeahh cmp al,'1' ;a 1 ? je bindo ;
yeahh jmp errout ;
noo, not binary input, skip this bindo loop isbin call binascbin ;
make ascii bin jmp output ;
do the output job dohex: mov si,inbuff ;
check for valid HEX input mov bx,errt2 ;
proper text ishex: lodsb ;
get the char cmp al,'0' jb errout and al,0dfh ;
force UPPERCASE cmp al,'F' ;
>F ? ja errout ;yeahh, dump this loop ishex call hexbin ;
make hex bin jmp output dodec: mov si,inbuff ;
check for valid DEC input mov bx,errt3 isdec: lodsb cmp al,'9' ;
>9 ? ja errout ;
yeahh, skip loop isdec lodsb call decbin ;make dec bin output: call
showit ;
show the stuff jmp quit errout: call write ;land here if error quit:
mov ax,exit int msdos ;
*****************************... ;
* Convert ascii decmimal to 32bit binary ;
* Input command line buffer, output EDX ;
*****************************... decbin: mov si,inbuff ;
command line buffer xor ebx,ebx ;
clear binary output ascdecbin: lodsb ;
get ascii value into AL cmp al,'0' ;ASCII to decimal jb noascii ;
exit if invalid ASCII cmp al,'9' ;test for highest value ja
noascii ;exit if larger than 9 sub al,30h ;
make bin decimal cbw ;byte to word cwd ;
word to dword push eax ;save digit on stack mov eax,ebx ;
move into output register mov ecx,10 ;
decimal multiplier mul ecx ;AX = AX * 10 mov ebx,eax ;
move product to output register pop eax ;
restore decimal digit add ebx,eax ;
add in digit jmp ascdecbin noascii: mov edx,ebx ;
copy to EDX ret ;*****************************... ;
* Convert ascii hex to 32 bit binary ;
* Input = command line buffer, output EDX ;
*****************************... hexbin: mov si,inbuff ;
pointer command line buffer xor edx,edx ;
clear binary output aschexbin: lodsb cmp al,'0' ;< 0 jb notasc ;
yes invalid character cmp al,'9' ;<= 9 jbe astrip ;
yes, strip high 4 bits and al,05fh ;
force upper case cmp al,'A' ;< ascii A jb notasc ;
yes, invalid character cmp al,'F' ;> ascii F ja notasc ;
yes, invalid character add al,9 ;ok, add 9 for strip astrip: and al,
0fh ;
strip high 4 bits mov cx,4 ;set shift count shl edx,cl ;
rotate EDX 4 bits xor ah,ah ;
zero out AH cbw add edx,eax ;
add digit to value jmp aschexbin ;continue notasc:
ret ;*****************************... ;
* Convert ascii binary to 32bit binary ;
* Input command line buffer, out EDX ;
*****************************... binascbin: mov di,inbuff ;
get command line mov si,di ;
get a copy bdump: cmp byte [di],0 ;check out length of arg je dok inc
di jmp bdump dok: mov ebx,2 ;
2 = binary base value mov ecx,1 ;multiplier xor eax,eax ;
clear that nextbin: cmp di,si ;
end of command line ? je binok ;
yeahh dec di ;next character to left mov dl,[di] ;
load the character sub dl,30h ;convert to decimal xor dh,dh ;clear
that push ecx ;
save ECX xchg eax,ecx ;
AX = multiplier mul edx ;d
igit value * multiplier add ecx,eax ;
add to partial value pop eax ;
restore multiplier mul ebx ;base times multiplier xchg eax,ecx ;
EAX = partial value jmp nextbin ;
go do next digit binok: mov edx,eax ;
save result in EDX ret ;
*****************************... ;
* Convert binary 32bit to hex ascii ;
* Input EDX, output numbuff ;
*****************************... binhex: push edx mov di,numbuff ;
output buffer mov cx,8 ;
count 8 digits hexit: push cx ;
save counter mov cl,4 ;one nibble rol edx,cl ;
rotate source left mov al,dl ;
move digit into AL and al,15 ;
clear high nibble daa ;adjust AL if A-F add al,240 ;
bump the carry adc al,40h ;convert hex to ASCII stosb ;
store in buffer pop cx ;
restore digit counter loop hexit ;
continue with next digit mov al,0 ;
null terminate buffer stosb pop edx ret ;
*****************************... ;
* Convert binary 32bit to decimal ascii ;
* Input EDX, output numbuff ;
*****************************... bindec: push edx mov esi,divtab ;
pointer subtract values mov di,numbuff ;
output buffer mov cx,10 ;
10 digits sublop: xor al,al ;
clear counter sblop: cmp edx,[esi] ;number < subractor ? jb tolow ;
yeah sub edx,[esi] ;noo, subtract add al,1 ;
incrase times subtracted jmp sblop ;
subtract again tolow: add al,30h ;make ascii stosb ;
store in buffer add si,4 ;
next subtractor loop sublop ;
do it mov al,0 ;
null terminate buffer stosb pop edx ret
;*****************************... ;
* Convert binary 32bit to binary ascii ;
* Input EDX, output numbuff ;
*****************************... binbin: push edx mov di,numbuff ;
output buffer mov cx,32
;32 bits bscan: mov al,'0' ;
assume that test edx,10000000000000000000000000... ;
bit set ? jz zero ;noo mov al,'1' ;
yeahh zero: stosb ;store in buffer shl edx,1 ;
shift in next bit loop bscan ;do next bit mov al,0 ;
null terminate stosb pop edx ret ;
*****************************... ;*
Show the results ;
*****************************... showit: showhex: mov bx,htext ;
show hex call write call binhex ;
convert mov si,numbuff call strip ;
get rid of leading zeroes mov bx,si call write ;
show value mov bx,lfeed ;cr,lf call write showdec: mov bx,dtext ;
show dec call write call bindec mov si,numbuff call strip mov bx,si
call write mov bx,lfeed call write showbin: mov bx,btext ;
show bin call write call binbin mov si,numbuff call strip mov bx,si
call write mov bx,lfeed call write ret ;
*****************************... ;
* Writes out the NULL terminated text supplied in BX. * ;
*****************************... write push edx mov si,bx ;
copy to SI mov cx,0 ;
clear count wloop lodsb ;
load AL with SI cmp al,0 ;
end of line ? je wdone ;
yeahh inc cx ;
no, incrase byte count jmp wloop ;
test next byte wdone mov dx,bx ;
text address in DX mov bx,1 ;
filehandle standard output = 1 mov ah,fwrite ;
MS-DOS write with handle is 40h int msdos ;
write to standard output pop edx ret ;
done ;*****************************... ;
Strip off leading zeroes in buffer at
SI ;*****************************... strip: lodsb ;
load byte cmp al,'0' ;
any leading zeroes ? jne nozero
;noo jmp strip ;
yeahh, check next nozero: dec si ;adjust that back one byte ret ;
*****************************... ;
* My kind of command line parsing. It just checks if theres ;*
any blankspaces between the options. The parameters ends up ;*
in the inbuff separated by 0:s, binary zeroes. ;
*****************************... parse: mov di,inbuff ;our buffer inc
si ;
dump that mov cx,1 ;were here, so we got one arg copy1: lodsb ;
load byte SI to AL cmp al,0 ;0 ?(end of line) je done ;
yeahh cmp al,32 ;SPACE ? je copy2 ;yeah stosb ;
noo, move AL to DI, incrase DI jmp copy1 ;
go on copy2: mov byte [di],0 ;
null terminate add cx,1 inc di ;
dump that byte(SPACE) jmp copy1 ;
back done: mov byte [di],0 ;
null terminate ret ;return ;
**************************** DATA STUFF
******************************... divtab: dd 1000000000 ;
subractor values dd 100000000 dd 10000000 dd 1000000 dd 100000 dd
10000 dd 1000 dd 100 dd 10
dd 1 ten dd 10 inbuff: times 128 db 0 ;
command line buffer numbuff: times 34 db 0 ;
number buffer utext:
db 'Convert (C) 1997 RonSoft.',13,10 db 'Usage:',13,10 db 'Convert .',
13,10 db 'H = Hexadecimal input',13,10 db 'D = Decmial
input',13,10 db 'B = Binary input',13,10,0 htext: db 'Hex: ',0 dtext:
db 'Dec: ',0 btext: db 'Bin: ',0 lfeed: db 13,10,0 errt1:
db "That's not binary.",13,10,0 errt2: db "That's not hexadecimal.",
13,10,0 errt3: db "That's not decimal.",13,10,0 cmdlen: db 0
END


(b) Write an 8086 assembly language program that will compute: 2.0*
log10 X for x = 0.1, 1.0, 10.0, 100.5, 1000.0, and six other values
using a loop. All values are in the single-precision (short real)
format. (5 Marks)
Ans.

DATA SEGMENT L4:
MOV OP2[BX],'$'
MOV AH,09H LEA DX,LINE INT 21H
MOV AH,00H LEA DI,OP1+3 LEA SI,OP2+3 LEA BX,
RESULT+4
MOV CX,04 CLC A:
MOV AL,[DI] ADC AL,[SI] DAA
MOV [BX],AL DEC SI DEC DI DEC BX LOOP A
MOV AL,0 ADC AL,0 MOV [BX],AL
MOV CL,04H LEA SI,RESULT+4 LEA DI,PRODUT+9 LOP1:
MOV AL,[SI] AND AL,0FH AND MULTPR,0FH MOV
BH,CL MOV CL,02H LL: MUL MULTPR AAM
ADD AL,[DI] AAA MOV [DI],AL
DEC DI MOV [DI],AH MOV AL,[SI] MOV BL,CL
MOV CL,04H SHR AL,CL AND AL,0FH
MOV CL,BL LOOP LL DEC SI
MOV CL,BH LOOP LOP1 MOV AH,09H LEA DX,PROMPT2 INT 21H
MOV CL,10 LEA DI,PRODUT DISP:
MOV DL,[DI] OR DL,30H MOV
AH,02H INT 21H INC DI LOOP DISP LAST:
MOV AX,4C00H INT 21H CODE ENDS
END
START

(c ) Write a program that will input a positive integer and print out
the list of its prime factors.
Comment on the run time of your algorithm and state any limitations
that you have imposed on the input integers. (5 Marks)
Ans.
Let's move on to the Prime Number program.
To find out if a number is prime, all we have to do is divide it by
every integer between 2 and (itself-1)
If we get an integer as an answer (no remainder), we have found a
factor,
and the number is not prime. However, to speed up the program a little
bit,
you will notice that factors always come in pairs, and the largest
factor can never be more than half of the number.
So to double the speed of the program, you only have to divide the
number by every integer from 2 to 1/2 of the number.
Here is the program (Notice how we test that the number is an integer
in lines 5 and 10):
GETNUM: PRINT "Enter an Integer between 2 and 20,000 and" PRINT "I
will tell you if it is a Prime Number"
INPUT NUM IF NUM - INT(NUM) <> 0 THEN GOTO GETNUM IF NUM < 2 THEN GOTO
GETNUM IF NUM > 20000 THEN GOTO
GETNUM PRIME = 0 FOR TRY = 2 TO NUM / 2 IF NUM / TRY <> INT(NUM / TRY)
THEN GOTO TRYMORE PRINT "Factor Found:"; TRY
PRIME = 1 TRYMORE: NEXT TRY IF PRIME = 1 THEN GOTO NOTPRIME PRINT
"Number is Prime!" GOTO DONE NOTPRIME: PRINT
"Sorry, Number is not prime." DONE: END There is one "trick" in this
program, and you may have noticed it in the variable PRIME.
This is what is called a FLAG variable. At the beginning of the
program, this flag is set to zero, which in our case means we have not
yet found a factor. We then divide the number by the first integer
(2). If it does not turn out to be a factor, it jumps down to the NEXT
command, and tries the next integer. On the other hand, if 2 is a
factor, then the variable PRIME is assigned the value of 1, indicating
we have found a factor, so we run it up the flag pole and see if
anyone salutes. Got it? When the FOR...NEXT loop has finished, we drop
down to line 15. If the entire loop has gone without finding a factor,
then lines 11 and 12 would never have been executed, and PRIME would
still be zero. We then drop down to line 16 and print out that the
number is prime. If PRIME is 1, then line 15 is true, and the program
branches to line 18, stating to the user that the number isn't prime.
Notice also that line 11 prints out the factors as it finds them
(d) Write an assembly language program to implement the queue
management.
Ans.
ASSUME
CS:CODE,DS:DATA,SS:STACK,ES:EXTRA ;**************************************************************************************** ;
SEGMENTS ;****************************************************************************************
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;
STACK SEGMENT OF
SIZE 256
BYTES ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
STACK
SEGMENT STAKDATA DB 100H DUP(00) ;reserve 256 bytes for stack and
initialize to 0. STACK ENDS
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;
EXTRA SEGMENT
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EXTRA SEGMENT ;
queue buffer for storing the input; QUEUE DB 1000H DUP(00) ; Queue
length assumed to be unlimited EXTRA ENDS
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;
DATA SEGMENT
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DATA SEGMENT
;constants ENTER EQU 0DH BACKSPACE EQU 08H ;messages INTRO DB 0DH,
0AH,"
QUEUE MANAGEMENT PROGRAM ",
0DH,0AH,"$"
INFO DB 0DH,0AH,0DH,0AH,"
ENTERED CHARS -
WHITE COLOR, DEQUEUED CHARS - RED COLOR ","$" ARRIVAL DB 0DH,0AH,"
ENTER DURATION OF DATA ARRIVAL ( IN SEC ) [MAX - 3600] : ","$"
ENQRATE DB 0DH,0AH," ENTER ENQUEUE RATE OF CHARACTERS ( CHARS/SEC )
[MAX - 255] : ","$"
DQRATE DB 0DH,0AH,"
ENTER DEQUEUE RATE OF CHARACTERS ( CHARS/SEC ) [MAX - 18] : ","$"
QADDR DB 0DH,0AH," ENTER QUEUE SEGMENT ADDRESS [MAX - 9999] : ","$"
QUIT DB 0DH,0AH," U CAN GRACEFULLY EXIT FROM THE PROGRAM BY PRESSING
ENTER ",0DH,0AH,"$" INPUT DB
0DH,0AH,"
ENTER INPUT : ","$"
TIMEOVER DB 0DH,0AH," TIME OUT.. U CAN ENTER NO MORE DATA ",
0DH,0AH,"$" DQREMAIN DB 0DH,0AH,"
DEQUEUING REMAINING CHARACTERS.... ","$"
DQALL DB 0DH,0AH,"
ALL CHARACTERS DEQUEUED ","$"
CAUTION DB 0DH,0AH," ** NO CHARACTERS ENTERED IN,
IN THE SPECIFIED DURATION **","$" THANKS DB 0DH,0AH,"
END OF PROGRAM - THANK U
",0DH,0AH,"$" ;
error messages ERRMSG DB 0DH,0AH,"
INVALID DATA - PLEASE RE ENTER ",0DH,0AH,"$" ZEROERR DB 0DH,0AH,"
0 IS NOT A MEANINGFUL DATA, PLEASE RE ENTER ",
0DH,0AH,"$" LARGE DB 0DH,0AH," INPUT NOT WITH IN SPECIFIED LIMITS
","$" ;
variables associated with input ERROR DB ? ;
variable used to set to 1 when error seen in input BUFFER DB 10
DUP(00) ;
buffer to store the number ;variables associated with Queue Program
DURATION DW 0 ;
maximum input duration in seconds ENQUEUE_RATE DB 0 ;
enqueue rate of characters in chars/sec DEQUEUE_RATE DB 0 ;
processing rate in chars/sec CHAR_TICKS DB 0 ;number of clock ticks
used for a character LATEST_DQ_TIME DW 0 ;
keeps track of the latest dequeue time ;
helper variables ENDTIME DW 0 ;the time at which program stops
accepting data from the user COUNT DB 0 ;
for keeping track of the number of elements in the Queue
ENTERED_CHAR_COUNT DB 0 ;
this variable equivalent to QREAR and ;
keeps track of the number of characters entered by the user
DISP_CHAR_COUNT DB 0 ;
this variable equivalent to QFRONT and ;keeps track of the characters
displayed on the console DATA ENDS;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;
CODE SEGMENT ;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CODE SEGMENT START: MOV AX,DATA MOV DS,AX ;
initializing data segment MOV AX,STACK MOV SS,AX ;
initializing stack segment MOV SP,OFFSET STAKDATA ; initializing stack
pointer CALL INIT ; initialize the parameters required for
Queue Management ;
find the number of ticks corresponding to one char [ INT 1AH,01H
used ] MOV BL,DEQUEUE_RATE MOV AX,18 ;
this is claculated to dequeue the appropriate number DIV BL ;
of characters after the elapsed time of a second MOV CHAR_TICKS,AL ;
Now CHAR_TICKS stores the number of ticks (out of 18) ;
corresponding to a single character ;
set system clock counter to 0 MOV AH,01H MOV CX,0H MOV DX,0H INT 1AH ;
start of the program ;
compute the endtime MOV AX,DURATION MOV BL,18 ; INT 1AH function
increments the counter 18 times per second MUL BL MOV
ENDTIME,AX READ: ;
get the current time MOV AH,0H INT 1AH ; check whether the specified
time reached CMP DX,ENDTIME JGE TIMEOUT ;
read the input using direct console I/O function INT 21H, 06H MOV AL,
00H MOV AH,06H MOV DL,0FFH ;read input from the
keyboard INT 21H ;
if no input AL=0 else AL=given character CMP AL,0H JZ NOINPUT ;if no
input dequeue characters CMP AL,ENTER ;
by pressing enter u can exit from the program JZ BYE1 ;
As for console read, no echo is produced, explicitly echo the
character MOV DL,AL ;
echos character in DL register on the display INT 21H CALL
ENQUEUE ;enqueue the recently read character NOINPUT: ;get the
time MOV AH,0H INT 1AH ;check whether one dequeue period elapsed MOV
CX,DX SUB DX,LATEST_DQ_TIME CMP
DL,CHAR_TICKS JGE DQ1 ;
if one dequeue period elapsed, dequeue the data JMP READ ;
else take more input DQ1: MOV LATEST_DQ_TIME,CX ;
update the latest dequeue time CALL DEQUEUE JMP READ ;display timeout
message TIMEOUT: MOV DL,' ' ;
this display has no meaning except to change the display MOV AH,06H ;
color to white INT 21H MOV DX,OFFSET TIMEOVER MOV AH,09H INT 21H BYE1:
MOV DL,' ' ;
this display has no meaning except to change the display MOV AH,06H ;
color to white INT 21H CMP COUNT,0 JZ BYE ;no further characters to
dequeue ;
display message " dequeuing remaining characters " MOV DX,OFFSET
DQREMAIN MOV AH,09H INT 21H ;
process if any characters are remaining in the Queue DQAGAIN: CALL
DEQUEUE CMP COUNT,0 JNE DQAGAIN BYE: CMP
ENTERED_CHAR_COUNT,0 ;
check whether any character is entered or not JZ BELOW1 MOV DL,' ' ;
this display has no meaning except to change the display MOV AH,
06H ;color to white INT 21H ;
inform the user that all the characters are dequeued MOV DX,OFFSET
DQALL MOV AH,09H INT 21H JMP THANK BELOW1: ;
caution the user that no characters entered in, in the specified
duration MOV DX,OFFSET CAUTION MOV AH,09H INT 21H ;
display end of the program message THANK: MOV DX,OFFSET THANKS INT
21H ;
finally exit from the program MOV AH,4CH ;
function call to return to DOS prompt INT 21H ;
------------------------------------------------------------------------------------- ;
ENQUEUE PROC NEAR ; IN : character in AL register ;
OUT : enqueues the recently read element at end of the queue ;
USES : ;
this routine is to add the elements in to the Queue ;
----------------------------------------------------------------------------------
ENQUEUE PROC NEAR INC COUNT ;
updating the count value XOR BX,BX MOV BL,ENTERED_CHAR_COUNT ;
ENTERED_CHAR_COUNT is equivalent to the QREAR MOV SI,OFFSET QUEUE MOV
BYTE PTR [SI+BX],AL ;
increment the character count INC ENTERED_CHAR_COUNT RET ENQUEUE
ENDP ;
------------------------------------------------------------------------------------- ;
DEQUEUE PROC NEAR ;
IN : DQNUM variable, which indicates the number of characters that can
be dequeued ;
OUT : dequeues the element at head of the queue and displays it on the
console ;
USES : ;
this routine is to add the elements in to the Queue ;
---------------------------------------------------------------------------------
DEQUEUE PROC NEAR CMP COUNT,0 JZ NODATA ;
if no data exit DEC COUNT ;
updating the count value XOR BX,BX MOV BL,DISP_CHAR_COUNT ;
DISP_CHAR_COUNT is equivalent to the QFRONT MOV SI,OFFSET QUEUE MOV
AL,BYTE PTR [SI+BX] ;
increment the character count INC DISP_CHAR_COUNT ;
dispaly the character on the console using red color XOR BH,BH ;bh=0,
video page MOV BL,4 ;
video red foreground color MOV AH,09H ;write character & attribute MOV
CX,1 ;
print the character for one time INT 10H ;
video BIOS service CALL PUTCHAR ;
this is called to advance the cursor by one position whereas ;above
written function call used for setting color NODATA: RET
DEQUEUE ENDP ;
------------------------------------------------------------------------------------- ;
INIT PROC NEAR ; IN : nothing ; OUT : initializes all the required
parameters used for queue management ;
USES : general DOS interrupts ; CALLS : calls the procedure INPUTNUM
to input a positive integer ;
this routine is meant for initializing the Queue parameters ;
----------------------------------------------------------------------------------
INIT PROC NEAR ;
display purpose of the program MOV DX,OFFSET INTRO MOV AH,09H INT
21H ;
take input from the user ;
input duration up to which data can arrive A1: MOV BX,3600 ;
maximum value specified in BX register MOV DX,OFFSET ARRIVAL MOV AH,
09H INT 21H CALL INPUTNUM CMP ERROR,1 JE A1 ;
if error again prompt for input MOV SI,OFFSET DURATION MOV WORD
PTR[SI],AX ;
input worst case inter arrival time E1: MOV BX,255 ;
maximum value specified in BX register MOV DX,OFFSET ENQRATE MOV AH,
09H INT 21H CALL INPUTNUM CMP ERROR,1 JE E1
;
if error again prompt for input MOV ENQUEUE_RATE,AL ;
input character processing time D1: MOV BX,18 ;
maximum value specified in BX register MOV DX,OFFSET DQRATE MOV AH,
09H INT 21H CALL INPUTNUM CMP ERROR,1 JE D1 ;
if error again prompt for input MOV DEQUEUE_RATE,AL ;
input queue segment address Q1: MOV BX,9999 MOV DX,OFFSET QADDR MOV AH,
09H INT 21H CALL INPUTNUM CMP ERROR,1
JE Q1 MOV ES,AX ;
display the colors information MOV DX,OFFSET INFO MOV AH,09H INT 21H ;
display the quit message on the console MOV DX,OFFSET QUIT MOV AH,09H
INT 21H ;
prompt the user to enter input MOV DX,OFFSET INPUT MOV AH,09H INT 21H
RET INIT ENDP ;
------------------------------------------------------------------------------------- ;
INPUTNUM PROC NEAR ; IN : maximum value that can be accepted is
specified in BX register ;
OUT : positive decimal number is stored in AX register ; USES :
VALIDATE, GETCHAR , PUTCHAR procedures ;
If the overflow of I/P occurs, the last character in the buffer is
overwritten. ;
backspace is handled ;
----------------------------------------------------------------------------------
INPUTNUM PROC NEAR PUSH BX MOV SI,OFFSET BUFFER ;
BUFFER is the address where the number is initially stored XOR BX,BX ;
clearing BX register MOV CH,0 ;
to keep track of the no. of characters NEXT: XOR AX,AX ;
clearing AX register CALL GETCHAR CMP AL,ENTER ;enter implies end of
the digit JE EXIT ;
decimal number stored in the buffer as an array of characters CMP
AL,BACKSPACE JNE N2 CMP CH,0 ;
handling the backspace that occurs first, before entering the number
JE NEXT B1: CMP CH,4 JG B2 JE J1 MOV AL,BACKSPACE
CALL PUTCHAR J1: MOV AL,' ' ;
display space at the cursor position CALL PUTCHAR MOV AL,BACKSPACE
CALL PUTCHAR DEC CH DEC SI JMP NEXT ;
max of 4 digits reached handling buffer overflow B2: MOV AL,' ' ;
display space at the cursor position CALL PUTCHAR MOV CH,4 MOV
AL,BACKSPACE CALL PUTCHAR JMP NEXT N2: INC CH
CMP CH,3 JLE N4 CMP CH,4 JE N3 DEC SI MOV BYTE PTR[SI],AL INC SI MOV
AL,BACKSPACE CALL PUTCHAR JMP NEXT N3:
MOV BYTE PTR[SI],AL;
place the character in buffer INC SI MOV AL,BACKSPACE ;
placing the cursor backwards to handle the overflow CALL PUTCHAR JMP
NEXT N4: MOV BYTE PTR[SI],AL ;
place the character in buffer INC SI JMP NEXT EXIT: MOV BYTE
PTR[SI],'$' ;
move termination character POP BX CALL VALIDATE ;
validate the entered number RET INPUTNUM ENDP ;
------------------------------------------------------------------------------------- ;VALIDATE
PROC NEAR ; IN : number stored in buffer, maximum value
that can be accepted is specified ; in BX register ; OUT : positive
decimal number is stored in AX register ;
sets ERROR variable to 1 if error seen in input ; CALLED BY :
INPUTNUM procedure ;
validates the entered decimal number and converts it to hexadecimal
number ;
the decimal can have a maximum of 4 digits and is stored in AX
register ;
----------------------------------------------------------------------------------------
VALIDATE PROC NEAR PUSH BX MOV SI,OFFSET BUFFER
MOV ERROR,0 XOR BX,BX ;
clearing registers XOR AX,AX XOR DX,DX XOR CX,CX MOV CL,0 ;
compute the hexa decimal number for entered number V1: MOV AL,BYTE
PTR[SI] V3: CMP AL,'$' ;
'$' is the terminating character JE EXIT1 ;
decimal number stored in AX CMP AL,'0' JB ERR ;any digit in the no.
would be >=0 and <=9 CMP AL,'9' JA ERR SUB AL,'0' ;
get decimal digit PUSH AX ;
save digit MOV AX,10 MUL BX MOV BX,AX POP AX XOR AH,AH ADD BX,AX INC
SI INC CL JMP V1 ;
get the next digit EXIT1: MOV AX,BX ;
storing the number in AX register POP BX CMP CL,0 JE RENTER CMP AX,
0H ;
check whether data entered is zero JZ ZERO ;
check whether the data is out of specified range CMP AX,BX ;
check whether the data entered is out of range JG OVERFLO RET ;
display that the input is not with in the specified range OVERFLO: MOV
DX,OFFSET LARGE JMP FUNC ;
display message '0'is not an acceptable data ZERO: MOV DX,OFFSET
ZEROERR JMP FUNC;
dispaly the invalid data message ERR: POP BX MOV DX,OFFSET ERRMSG
FUNC: MOV AH,09H INT 21H ;
if error again prompt for entering a valid number RENTER: MOV ERROR,1
; setting error bit to 1 RET VALIDATE ENDP ;
---------------------------------------------------------------------------------------- ;
GETCHAR PROC ;
IN : none ; OUT : character stored in the AL register ;
USES : keyboard BIOS interrupts ; to input a character from
keyboard ;
----------------------------------------------------------------------------------------
GETCHAR PROC NEAR PUSH BX INT 16H ;
keyboard BIOS service ;
character is stored in AL register ;
donot echo the character enter, backspace CMP AL,ENTER JZ GO CMP
AL,BACKSPACE JZ GO ;
echo the character on video MOV BH,0 ;video page MOV BL,1
;fore-ground video color MOV AH,0EH ;
tele-type INT 10H ;video BIOS service GO: POP BX RET GETCHAR ENDP ;
---------------------------------------------------------------------------------------- ;
PUTCHAR PROC ; IN : ASCII value of the character in AL register ;
OUT : displays character on the monitor ;
USES : video BIOS interrupts ; to output a character to video
dispay ;
----------------------------------------------------------------------------------------
PUTCHAR PROC NEAR PUSH BX XOR BH,BH ;
bh=0, video page MOV BL,1 ;video foreground color MOV AH,0EH ;
tele-type which advances cursor automatically INT 10H ;video BIOS
service POP BX RET PUTCHAR ENDP CODE ENDS
END
START ;

From: Frank Kotler on
Umesh wrote:
> (a) Write an assembly language program to accept a decimal number and
> display it's two's complement representation in binary and hexadeimal
> formats. (5 Marks)
> Ans.

....
> db 'Convert (C) 1997 RonSoft.',13,10

Nice job, Ron!

> (b) Write an 8086 assembly language program that will compute: 2.0*
> log10 X for x = 0.1, 1.0, 10.0, 100.5, 1000.0, and six other values
> using a loop. All values are in the single-precision (short real)
> format. (5 Marks)
> Ans.
>
> DATA SEGMENT
> L4:
> MOV OP2[BX],'$'
> MOV AH,09H
> LEA DX,LINE
> INT 21H
....

I'm not so sure how this one works. Can't get it to assemble. What's
"LINE"? I don't think this is complete!

> (c ) Write a program that will input a positive integer and print out
> the list of its prime factors.
> Comment on the run time of your algorithm and state any limitations
> that you have imposed on the input integers. (5 Marks)
> Ans.

....
> INPUT NUM IF NUM - INT(NUM) <> 0 THEN GOTO GETNUM IF NUM < 2 THEN GOTO
> GETNUM IF NUM > 20000 THEN GOTO
> GETNUM PRIME = 0 FOR TRY = 2 TO NUM / 2 IF NUM / TRY <> INT(NUM / TRY)
> THEN GOTO TRYMORE PRINT "Factor Found:"; TRY
> PRIME = 1 TRYMORE: NEXT TRY IF PRIME = 1 THEN GOTO NOTPRIME PRINT
> "Number is Prime!" GOTO DONE NOTPRIME: PRINT
> "Sorry, Number is not prime." DONE: END
....

Doesn't really say that this one is to be in asm, does it? Bad
assumption on my part. Always read the question carefully!

> (d) Write an assembly language program to implement the queue
> management.
> Ans.
> ASSUME CS:CODE,DS:DATA,SS:STACK,ES:EXTRA
....

So, uhhh... what fraternity do I have to pledge to get this code?

Best,
Frank

 | 
Pages: 1
Prev: URGENT
Next: masm linking from console