• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4443
  • Last Modified:

Assembly Program to convert to any base

I'm trying to write an assembly program that will take two integer inputs. One must be a number to be converted and one must be the base to convert to. Right now my program is hanging up after one input. My code is posted below and I tried to comment it the best i could. If someone could edit it to do what it needs to do, that would help a lot!

.model small
.386
.stack 100h
.data
.code


decimal                  proc            near

;print out number given in AX in decimal

                  xor            eax,eax
                  xor            ebx,ebx
                  mov            cx,2            ;this is to accept the base and the decimal value
                  
newchar:            
                  cmp            cx,0
                  jle            startConvert
                  dec            cx
                  mov            ah,1            ;This sets up to accept another input from keyboard
                  int            21h            ;this asks for an input and stores it in al
                  
                  ;mov            al,0h            ;This sets up to accept another input from keyboard
                  ;int            21h            ;this asks for an input and stores it in al
                  ;push            ax            ;stores the second input from keyboard
                  
                  
                  
                  sub            al,30h
                  jl            stloop            ;jumps if negative
                  cmp            al,9h
                  jg            stloop            ;jumps if greater than
                  
                  xor            ah,ah            ;to get only the digit they type
                  push            ax            ;this stores the first input from the keyboard
                  
                  cbw                        ;convert byte to word
                  ;jmp            newchar            ;to accept multiple digits

stloop:                  
                  mov            ah,2
                  mov            dl,0ah
                  int            21h            ;This section inserts a carrige return
                  mov            dl,0dh
                  int            21h
                  
                  
startConvert:            pop            bx
                  push            ax            ;given to routine to print out
                  ;push            bx
                  push            cx
                  push            dx
                  
                  mov            cx,0            ;starts a counter for the number of digits
                  ;mov            bx,10            ;base 10 conversion THIS IS THE LINE TO CHANGE FOR DIFFERENT BASES!!!!
                  
nonzero:            
                  xor            dx,dx            ;same as mov dx,0
                  div            bx
                  push            dx            ;this is the remainder of the divider
                  inc            cx            ;this will help determine the number of digits produced
                  or            ax,ax            ;sets flags to check is ax = 0
                  jne            nonzero            ;jump not equal to

write:                  
                  pop            dx            ;gets deciaml digit from stack
                  add            dl,'0'            ;ascii value of 30h THIS ALSO NEEDS TO CHANGES FOR DIFFERENT BASES!!!
                  mov            ah,2
                  int            21h
                  loop            write            ;loops number of times in cx, which is the number of digits
                  pop            dx
                  pop            cx
                  pop            bx
                  pop            ax
                  ret
decimal                  endp
                  end decimal
                  
0
jer2eydevil88
Asked:
jer2eydevil88
1 Solution
 
DawafflemanCommented:
i think the problem might be that your popping without pushing, and viceversa. for example:
----------------------------------------------------------------
            sub          al,30h
               jl          stloop          ;jumps if negative
               cmp          al,9h
               jg          stloop          ;jumps if greater than
               
               xor          ah,ah          ;to get only the digit they type
               push          ax          ;this stores the first input from the keyboard <---push
               
               cbw                    ;convert byte to word
               ;jmp          newchar          ;to accept multiple digits

stloop:              
               mov          ah,2
               mov          dl,0ah
               int          21h          ;This section inserts a carrige return
               mov          dl,0dh
               int          21h
               
               
startConvert:          pop          bx                                                         <----pop
--------------------------------------------------------


so im assuming that you wanted that value in ax to be popped off the stack into bx, but that value isnt necessarilly going to be on the stack because if you jump here:

           sub          al,30h
               jl          stloop          ;jumps if negative
               cmp          al,9h
               jg          stloop          ;jumps if greater than

then you never get to the point where ax is pushed onto the stack, and if this is a procedure then you will just pop the return address off the stack and next time you push something it will go on the stack in place of it.
im not sure if this is the only problem or not, but it seems to be one regardless.

Also in the 'nonzero' loop you push dx for each iteration of the loop, but you dont pop dx for each iteration, which is fine if the loop only goes through once, but if it goes through like ten times then the rest of your stack will be way out of wack.
0
 
Jase-CoderCommented:
say you have the number 1234 in base 10 to convert to any base you do the following:

 (1 *  BASE^3) + ( 2 *BASE^2)  + (3 * BASE^1) + (4 * BASE^0)

so if you wanted to do base 7 you just make BASE = 7, Also if you want to convert to some odd base like 4 change BASE to 4
0
 
mbizupCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: Dawaffleman {http:#13889461}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mbizup
EE Cleanup Volunteer
0
 
Neil ManlimosCommented:
I'm not good at assembly program. Can you please help me how to make an assembly program which accepts two digit input as base 12 and converts it to a decimal with a yes or no for continuation of the program.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now