Need Assembly Programming Help (TASM) - Booth's Algorithm

Tag: algorithm , assembly , low-level , tasm Author: gaohaoyang80 Date: 2009-11-12

I've written an algorithm to simulate Booth's Algorithm using only Add, Sub, and Logical Operators and return a hexadecimal value. My Tasm compiler keeps throwing me these errors. When I try to omodify the code, it still doesn't work. Could someone help me out please.

(29) Extra characters on line
(38) Illegal immediate
(44) Illegal immediate
(52) Underfined symbol: RES2
(126) Expecting pointer type

;Booths Algorithm


.model small


 prompt db 13,10,"Enter first number to multiply. $"
 prompt2 db 13,10,"Enter second number to multiply. $"
 res db 13,10,"The answer is $"

 ans dw 2
 hold db 0 



mov ax,seg prompt,prompt2,res,ans,hold,n1,n2
mov ds,ax

mov ah,09h
mov dx,offset prompt                     
int 21h

call read                

mov n1,bl            
mov ah,09h
mov dx, offset prompt2           
int 21h

call read                
mov n2,bl            

call Algorithm              

 mov [ans],ax          
 mov bx,ax

 mov dx,offset res2             
 mov ah,09h
 int 21h

 call write              

 mov ah,4ch
 int 21h


 mov ah,00h             
 mov [hold],bl

 mov al,01h              
 int 21h                 
 cmp al,0dh             
 je Copy                   
 mov cl,al              
 sub cl,30h            
 mov al,[hold]           
 mov bl,0ah              
 mul bl                  
 mov [hold],al           
 add [hold],cl           
 jmp f0                 

Copy :
 mov bl,[hold]           

 mov ah,0              
 mov al,n1         
 mov cx,8                
 mov bh,n2         

 mov bl,al      
 and bl,1                
 jnz f2               
  JNC f3               
  sub ah,bh               
 jmp f3

 jc f3
 add ah,bh

  shr ax,1
  loop f1

 mov al,bl
 lea di,[ans]
 mov bh,0ah
 mov cl,24h
 mov [di],cl
 dec di

 mov ah,00h
 div bh
 add ah,30h
 mov [di],ah
 dec di
 cmp al,00h
 jnz 4
 inc di
 mov ah,09h
mov dx,di
 int 21h

end start
It'd be kinda cool if you helped us out a bit and showed us which line numbers those are in the code.
The 4th error is quick to spot, you typed res2, but you only have res.
This line is completely invalid: mov ax,seg prompt,prompt2,res,ans,hold,n1,n2. It would have to be just move ax,data. You should also use assume cs:code, ds:data, ss:stack
I just gotta ask, why asm?? Just write what you need in C and decompile it if you need to, if your really concerned about performance, write it in C, profile it, find the bottle neck, and write that part of the C code in asm. It will save you days of work.

Other Answer1

My asm is a little rusty, but you can try these changes:

line 29:

mov ax,@data  ; should pick up the address of the data segment

or alternately:

mov ax, seg prompt   ; seg takes only one variable, not multiple...

line 38:

mov [n1],bl      ; memory addresses need square brackets

line 44:

mov [n2],bl      ; memory addresses need square brackets

line 52:

mov dx,offset res    ; don't know where res2 came from

line 126 - I'm not sure what's going on here...


After taking into consideration what you said I modified my code but the mov [n1],bl mov [n2],bl Still gives me: >illegal Immediate Below is the modification I made for the first error: mov ax,seg prompt;,prompt2,res,ans,hold,n1,n2 mov ax,seg prompt2 mov ax,seg res mov ax,seg ans mov ax,seg hold mov ax,seg n1 mov ax,n2 mov ds,ax
You don't need that many "mov ax, " commands... The aim of those commands is to load the address of the data segment into the ds register... Only one is needed.

Other Answer2

Errors in your code:

This line is completely invalid:

mov ax,seg prompt,prompt2,res,ans,hold,n1,n2.

It would have to be just:

move ax,data

You should also include this right before start::

assume cs:code, ds:data, ss:stack

These are invalid as well, since you wanted to define memory variables, I guess:


It should be:

n1 db 0
n2 db 0

When you access n1 and n2 write it this way, as Stobor already noted:

mov [n1],bl
mov [n2],bl

All variable references done by "addressing" in assembly, so the square brackets.

And you didn't define RES2 at all as noted in a comment already.

Hope this helps.

See also:

Add a comment here if something is not clear enough.


Thanks for the input. When I added "assume cs:code, ds:data, ss:stack" before the "start:" I got numerous (12) errors of the same type "Undefined symbol: CODE".