-2

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
anila
  • 119
  • 2
  • 3
  • 8
  • 2
    Hint: `mul bl` multiplies `al` so check you have the correct operand in there... PS: comment your code especially if you want others to help and learn to use a debugger. Also, read the instruction set reference. – Jester Dec 19 '16 at 18:30
  • 1
    Maybe you don't understand difference between characters on screen, and values in registers. For example you read single char from input. If you did enter digit "5", the `al` will contain value `53`. That's value defined in ASCII encoding as glyph "digit 5". Check ASCII table. You are actually converting result of `MUL` by adding `48` (glyph "0"), but if MUL is multi-digit, this will fail (`10+'0' = ':'`). And finally you loop to `TOP:` expecting the `AL` contains the digit from user, but that one is already lost, so for second iteration there will be something multiplied. Use **DEBUGGER**. – Ped7g Dec 19 '16 at 18:30
  • @joze now check ur inbox – anila Dec 19 '16 at 20:52
  • Now plz @joze correct me here – anila Dec 19 '16 at 20:53
  • 3
    With no comments in the code and no description of exactly what the output *should* be, this isn't a [mcve]. Use a debugger so you can ask a specific question about something in your code. – Peter Cordes Dec 19 '16 at 20:57
  • @ peter i want to enter number and then print the table for multiplicaion like if i enter 2 then it print 2,4,6,8,and so on upto 20 – anila Dec 19 '16 at 20:59
  • @ joze kindly now correct me here plz i want to enter number and then print the multiplicaiton table for that number upto 10 numberslike if i enter 2 then it print 2,4,6,8,upto 20 – anila Dec 19 '16 at 21:02

4 Answers4

1

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

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Enics
  • 11
  • 1
0

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.

Ped7g
  • 16,236
  • 3
  • 26
  • 63
  • @anila what do you think `MUL bl` does?? :-o – Ped7g Dec 19 '16 at 18:54
  • Also I answered this in my answer: **"use instruction reference guide"** ... it's often shorter and more accurate, than answers on stack overflow. – Ped7g Dec 19 '16 at 18:56
  • Yes... so ... what are you asking with *"How do i MUltiply AL with BL?"*, if you know the answer? `MUL bl` = `ax = al * bl`. – Ped7g Dec 19 '16 at 18:57
  • So do you already understand what is ASCII? When you output value `65`, the glyph `A` will be shown on screen. To show digit 0 you have to output value `48` (equal to `30h`, or `'0'` in many assemblers). You can't simply output numbers in DOS, there's no such service, only ASCII characters and string. To output a number you have to first convert it into ASCII string. http://stackoverflow.com/a/40505938/4271923 – Ped7g Dec 19 '16 at 19:01
  • 3
    Plus we are back to your previous question, so... looks like you didn't understand those answers. http://stackoverflow.com/q/41127123/4271923 – Ped7g Dec 19 '16 at 19:04
0

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
Community
  • 1
  • 1
0
.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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847