News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help Needed

Started by 2brian04, March 16, 2005, 04:35:48 PM

Previous topic - Next topic

2brian04

this is an assignment for uni and im rattleing my brains trying to figure it out .. any help would be appreacieted thanks

question is .... Explain why the string "xxxxx" appears several times . Modify the program in a manner which makes these strings unneccessary and provide a commented print out of your modifications.

i have tried just removing these "xxxxx" and the program works fine, but they must have a purpose

code for program...

   DOSSEG
   .MODEL   SMALL
   .STACK   100h
   .DATA
lf   EQU   0Ah
cr   EQU   0Dh
mess3   DB   lf,cr,"Processed output is: $"
mess1   DB   lf,lf,cr,"Type a single hex digit (0 to 9, A to F)"
   DB   lf,cr,"or any other key to terminate program: $"
mess2   DB   lf,cr,"Error in input. Program terminating.$"
h2b   DB   "0000$","0001$","0010$","0011$","0100$","0101$","0110$","0111$"
   DB   "1000$","1001$","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx","xxxxx"
   DB   "xxxxx","1010$","1011$","1100$","1101$","1110$","1111$"

   .CODE
start:   mov   ax,@DATA      
   mov   ds,ax         

next:   mov   dx,offset mess1      ;moves the address of mess1 to dx register
   call   disp         ;Calls disp
   call   rdkey         ;Calls rdkey
   call   check         ;Calls Check
   push   ax         ;Pushes both ah and al onto the stack
   mov   dx,offset mess3      ;moves the address of mess3 to dx register
   call   disp         ;Calls disp
   pop   ax         ;Pops both ah and al off the stack
   and   ah,ah         ;and's the ah register with the ah register and stores in the ah register
   jz   cont         ;jumps to cont if ah register is 0
   mov   dx,offset mess2      ;moves the address of mess2 to dx register
   call   disp         ;Calls disp
   mov   ax,4c00h      ;Terminate
   int   21h         ;Calls DOS

cont:   mov   ah,0         ;Moves 0 to the ah register
   mov   dx,offset h2b      ;Moves the address of h2b to dx register
   add   dx,ax         ;adds the ax register to the dx register and stores in the dx register
   add   dx,ax         ;adds the ax register to the dx register and stores in the dx register
   add   dx,ax         ;adds the ax register to the dx register and stores in the dx register
   add   dx,ax         ;adds the ax register to the dx register and stores in the dx register
   add   dx,ax         ;adds the ax register to the dx register and stores in the dx register
   call   disp         ;Calls disp
   jmp   next         ;jumps to next

check:   cmp   al,'0'         ;Compares the Hex code for 0 with the al register
   jl   bad         ;Jumps to bad if the hex code is less than the al register
   cmp   al,'f'         ;Compares the Hex code for F with the al register
   jg   bad         ;Jumps to bad if the hex code is greater than the al register
   cmp   al,'9'         ;Compares the Hex code for 9 with the al register
   jle   ok         ;Jumps to ok if the hex code is less than or equal to the al register
   cmp   al,'a'         ;Compares the Hex code for A with the al register
   jge   ok2         ;Jumps to ok2 if the hex code is greater than or equal to the al register

bad:   mov   ah,1         ;Moves 1 into the ah register
   ret            ;Pops the return address off the stack

ok:   mov   ah,0         ;Moves 0 into the ah register
   sub   al,30h         ;Subtracts 30h from the al register
   ret            ;Pops the return address off the stack

ok2:   mov   ah,0         ;Moves 0 into the ah register
   sub   al,50h         ;Subtracts 60h from the al register
   ret            ;Pops the return address off the stack

rdkey:   mov   ah,1         ;Read a key from the keyboard to AL register
   int   21h         
   and   al,7fh         
   ret            

disp:   mov   ah,9         ;Displays the string pointed to by the DX register on console
   int   21h         
   ret            
   END   start         

MichaelW

Your code, as posted, will not work for inputs "A" through "F". It is assuming without checking that the letters will be input in lower case.

The index value for the string table is calculated from the ascii code of the input character, with the result as follows:

"0" = 0
"1" = 5
"2" = 10
...
"9" = 45
"a" = 85
...
"f" = 110


Note the gap between "9" and "a".

One simple method of verifying the validity of a hex digit character and converting it to a value would be:

Convert any lower-case alphabetic characters to upper case.

Search for a match to the character in "0123456789ABCDEF".

The position of the match is the hex value of the character.

If there is no match then the character is not a valid hex digit.
eschew obfuscation

2brian04

Sorry that was a mistake, one of the questions in my assignment is to modify the code to accept lowercase (a - f) rather then uppercase (A - F)
So it should read "Type a single hex digit (0 to 9, a to f)"

The program works fine, both with the "xxxxx"'s in and without them in.
I just need to know the purpose of them and if needed any changes i need to make to compensate me removing them.

cheers
Brian

MichaelW

QuoteI just need to know the purpose of them and if needed any changes i need to make to compensate me removing them.

Yes, I understood that from your first post, and I attempted to answer both of these questions.

The "xxxxx" initializers serve as fillers to bridge the gap in the calculated index values (offset addresses) between "9" and "a". I outlined a method of eliminating the gap that would also replace the code that checks the range of the input characters, simplifying the whole process. Another method that would require minimal changes would be to subtract 57h (instead of 50h) from the character codes for the letters. This would place "a" at index 50, "b" at index 55, etc.
eschew obfuscation

2brian04

Excellent thank you very much :)