Print multiplication table assembly language

12.1k views Asked by At

Hi m using dosbox and masm compilor how can i print multiplication table my code is as follows but it prints special characters. KIndly correct me here m doing wrong. Correct the code if any one can help me through this

     .MODEL SMALL
     .STACK 100H
      .DATA
    MSG DB 'ENTER A DIGIT:$'
    NL DB 0DH,0AH,'$'
  .CODE
    MAIN PROC

   MOV AX,@DATA
  MOV DS,AX

  LEA DX,MSG
  MOV AH,09
  INT 21H

  MOV AH,01
 INT 21H



  XOR BX,BX
  MOV BL,1
   MOV CL,10
 TOP:
  MUL BL
 ADD AL,30h
 MOV AH,02
 MOV DX,AX
 INT 21H

LEA DX,NL
MOV AH,09
INT 21H

INC BL
LOOP TOP
JCXZ SKIP
SKIP:
 MOV AH,4CH
 INT 21H

 MAIN ENDP
 END MAIN
4

There are 4 answers

0
Jose Manuel Abarca Rodríguez On BEST ANSWER

Your multiplication results are numbers with more than one digit, so you will need a procedure to convert the number to string, for example, convert 21 to '21', because if you display 21 the screen will show the ASCII char 21 ('§').

I will add the procedure "number2string" to your code, this procedure takes two parameters : AX is the number to convert (in your case, the result of the multiplication), and SI with the address of a string variable :

     .MODEL SMALL
     .STACK 100H
      .DATA
    MSG DB 'ENTER A DIGIT:$'
    NL DB 0DH,0AH,'$'   
    NUM DB ?                        ;◄■■ NUMBER ENTERED BY USER.
    STRING DB '$$$$$$'              ;◄■■ STRING TO STORE NUMBER CONVERTED.
  .CODE
    MAIN PROC

   MOV AX,@DATA
  MOV DS,AX

  LEA DX,MSG
  MOV AH,09
  INT 21H

  MOV AH,01
 INT 21H             
  SUB AL,'0'    ;◄■■ CONVERT CHAR TO NUMBER.
  MOV NUM,AL    ;◄■■ PRESERVE NUMBER BECAUSE AL WILL BE DESTROYED.


  XOR BX,BX
  MOV BL,1
   MOV CX,10    ;◄■■ NOT CL.
 TOP:   
  MOV AL,NUM    ;◄■■ THE NUMBER TO CONVERT.
  MUL BL        ;◄■■ RESULT IN AX.

  PUSH BX            ;◄■■ BX AND CX WILL BE
  PUSH CX            ;◄■■ DESTROYED IN PROC. 
  LEA  SI,STRING     ;◄■■ STORE NUMBER CONVERTED.
  CALL NUMBER2STRING ;◄■■ CONVERT AX TO STRING.
  POP  CX            ;◄■■ RESTORE BX 
  POP  BX            ;◄■■ AND CX.

LEA DX,NL
MOV AH,09
INT 21H

LEA DX,STRING        ;◄■■ DISPLAY NUMBER CONVERTED.
MOV AH,09
INT 21H

INC BL
LOOP TOP         ;◄■■ CX-1, IF CX > 0 JUMP TO TOP.
;JCXZ SKIP       ;◄■■ UNNECESSARY.
;SKIP:           ;◄■■ UNNECESSARY.
 MOV AH,4CH
 INT 21H

 MAIN ENDP

 END MAIN
5
Ped7g On

KIndly correct me here m doing wrong.

You don't use debugger to verify what the code really does.

You don't use instruction reference guide to verify what the code really does.

You use assumptions and expectations while writing the code, but you don't document them in the source with comments. This makes it hard for anyone else to guess what is bug and what is not, because we see only instructions, but not your intentions. If you would comment every group of 1-5 instructions, what is their purpose, it would be easier for others to show you where your expectations break, because instruction does something what you didn't expect.


Plus you have few bugs in the code, but everyone has bugs in his code, that's normal. Before debugging and fixing.

0
Enics On

For you Query that i understood is that you want to print a multiplication table with proper output. for this you need to have broken multiplication values in two BCD numbers. have a look on this link The complete assembly program which prints output like this

Click on image to see output

3
AniX Sharma On
.model small
.stack 100h
.data
num db ?
msg db 'Enter a digit:$'
string db 5 dup(?)
.code
main proc
mov ax, @data
mov ds, ax

lea dx, msg      ;; display enter a digit:
mov ah, 9
int 21h
mov ah, 1            ;; gets a digit as character
int 21h
sub al, 48     ;; convert character into number
mov num, al

xor bx, bx
mov bl, 1
mov cx, 10
top:mov al, num           ;; multiplication step
mul bl
push bx                 ;; reserving bx and cx for later use by pushin stack
push cx
lea si, string

mov bx, 10          ;; storing 2 digits number as character in string
xor cx, cx
division: xor dx,dx
div bx
push dx
inc cx
cmp ax, 0
jne division
store: pop dx
add dl, 48
mov [si], dl
inc si
loop store
mov [si], '$ '

pop cx
pop bx
mov dl, 13             ;;; new line
mov ah, 2
int 21h
mov dl, 10
mov ah, 2
int 21h

lea dx, string       ;;get that 2 digit number which is character from string
mov ah, 9
int 21h

inc bl
loop top                 ;; multiplication table stops

mov ah, 4ch             ;; end of program
int 21h
main endp
end

enter image description here