Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,628 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,039 people online right now. Registration is fast and FREE... Join Now!




Assembly code program problem

 
Reply to this topicStart new topic

Assembly code program problem

toot
post 13 Dec, 2005 - 05:57 PM
Post #1


New D.I.C Head

*
Joined: 13 Dec, 2005
Posts: 1


My Contributions


Hi all,

Very new to this, and currently studying it part time. I have been supplied with program code to assemble in MASM (only allowed to use MASM, no other assembler) and am a bit stuck on 2 questions... baring in mind we have only really studied this for about an hour at the most i think its a bit cruel to say the least!
The program is a simple hex to binary converter

Here is the code:

CODE

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 on 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
      call disp
      call rdkey
      call check
      push ax
      mov  dx,offset mess3
      call disp
      pop  ax
      and  ah,ah
      jz   cont
      mov  dx,offset mess2
      call disp
      mov  ax,4c00h
      int  21h
cont:  mov  ah,0
      mov  dx,offset h2b
      add  dx,ax
      add  dx,ax
      add  dx,ax
      add  dx,ax
      add  dx,ax
      call disp
      jmp  next
check: cmp  al,'0'
      jl   bad
      cmp  al,'F'
      jg   bad
      cmp  al,'9'
      jle  ok
      cmp  al,'A'
      jge  ok
bad:   mov  ah,1
      ret
ok:    mov ah,0
      sub al,30h
      ret

rdkey: mov ah,1
      int 21h
      and al,7fh
      ret

disp:  mov ah,9
      int 21h
      ret
      END start



the questions are

Explain why the string "xxxxx" appears several times in lines 12 and 13 of the program. Modify the program in a manner which makes these strings unnecessary and provide a commented printout of your mods.
I've tried numerous things and nothing works.

Next question:

Modify the program so that it accepts and correctly processes the lower case alphabetic chars a to f as valid input instead of the upper case characters A to F.
Also tried numerous things with no success.

Anyone have any ideas?

Anything would be a great help!

Thanks
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 13 Dec, 2005 - 11:29 PM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,128



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


oh, eh, 8086 asm, ew

The best thing you can do untill someone better versed at 86asm, is run through the code step by step in a debugger. I think even the program debug in windows will let you do that. Using that you can view the registers at each step and get a feel for what the program is doing.

My advice for the second part of the question about lower case A-F, is find in the program where it is checking for uppercase A-F (thier hex values) and convert those values to those of the lower case.
User is offlineProfile CardPM

Go to the top of the page

sameergk
post 22 Jan, 2006 - 11:35 PM
Post #3


New D.I.C Head

*
Joined: 22 Jan, 2006
Posts: 2


My Contributions


The string "xxxxx" appears exactly seven times... This is to skip the ascii characters from 3A to 40. These characters appear in between the series 0-9 and A-F.


QUOTE

Here's the code to:
1. Remove all "XXXXX"
2. Processes even 'a' to 'f'


CODE


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 on input. Program Terminating.$"
h2b   DB "0000$","0001$","0010$","0011$","0100$","0101$","0110$","0111$"
    DB "1000$","1001$",; characters in ranfe 0-9
    DB "1010$","1011$","1100$","1101$","1110$","1111$"; A-F characters

.CODE

start: mov ax,@DATA
     mov ds,ax

next:  mov  dx,offset mess1
     call disp
     call rdkey
     call check
     push ax
     mov  dx,offset mess3
     call disp
     pop  ax
     and  ah,ah
     jz   cont
     mov  dx,offset mess2
     call disp
     mov  ax,4c00h
     int  21h
cont:  mov  ah,0
     mov  dx,offset h2b
     add  dx,ax
     add  dx,ax
     add  dx,ax
     add  dx,ax
     add  dx,ax
     call disp
     jmp  next
check: cmp  al,'0'  ; Check for Range 0-9
     jl   bad
     cmp  al,'9'
     jle  ok
     cmp  al,'f'  ; Check for Small 'f'
     jg  bad
     cmp  al,'a'
    jge ok1        ;Check for Range 'a' - 'f'
     cmp  al,'F'  ; Check for CAPS 'F'
     jg   bad
     cmp  al,'A'
    jge ok2        ;Check for Range 'A' - 'F'
bad:   mov  ah,1
     ret
ok1: sub al,'a'  ;; Indicates Range is 'a' to 'f'  
      add al, Ah ;; makes a = 10, f = 15
       ret
ok2: sub al, 'A' ;; Indicates Range is 'a' to 'f'
      add al, Ah ;;makes A = 10, F = 15
      ret
ok:    mov ah,0
     sub al,30h
     ret

rdkey: mov ah,1
     int 21h
     and al,7fh
     ret

disp:  mov ah,9
     int 21h
     ret
     END start
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 23 Jan, 2006 - 03:24 AM
Post #4


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Working for different sets of characters (ie. 'A' to 'F' and from 'a' to 'f') like the way sameergk did results in inefficiency and repetition of instructions [the add al,Ah gets repeated etc.]

In Assembly, Efficiency is of prime importance. You can do this instead to recude code size as well as reduce the clock cycles for a better performance.

Search for characters from 'A' to 'F'.
XOR them with 32 ( That's 20h ) to convert them to lowercase characters and then deal with them.

This post has been edited by born2c0de: 23 Jan, 2006 - 03:28 AM
User is offlineProfile CardPM

Go to the top of the page

sameergk
post 23 Jan, 2006 - 04:23 AM
Post #5


New D.I.C Head

*
Joined: 22 Jan, 2006
Posts: 2


My Contributions


To born2c0de
Code Please.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 03:45AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month