Author Topic: Nasm - Incorrect output  (Read 8652 times)

Offline ricky008

  • Jr. Member
  • *
  • Posts: 2
Nasm - Incorrect output
« on: March 25, 2017, 08:26:06 AM »
Hi,I'm new to assembly language and have been struggling to finish an assignment for days.Im using Nasm assembly language on a Windows 7 pc.I have done all of the coding but getting the incorrect output.The program must ask the user to input a number between 1 and 9,read every byte in the bit pattern array and the display the enlarged number on the screen,using the dollar character.The exact problem is the first byte in the array does not display as output,but the other bytes,diplays perfectly.So either my array indexing is incorrect or im using some register incorretly.In my code below im trying to display the number 5, hence use must enter 5 as input.

Code: [Select]
bits 16
org 0x100
jmp main

message: db                                   'Please enter a number between 1 and 9',0ah,0dh,'$'

arrnum5     db                                    1Fh,10h,1Ch,02h,01h,02h,1Ch ; enlarged 5

cr_lf: db 0ah,0dh,'$'        ;move cursor to beginning of next line

;Display string
Display: mov     ah,09h
int     21h
ret

;Display character
Display2: mov     ah,02h
int     21h
ret

;Input character
Input: mov     ah,07h
int     21h
ret

Print_num: 
               
        mov  cl,8                 ; cl counts the number of bits in the byte that has been read

Print:
cmp cl,0              ;if cl=0,then every bit in this byte has been read,
        je  Next                                     ;then jump to Next to get next byte


rcl si,1            ;rotate the byte that si points to, left by 1 bit and check if
                                                   ;the carry flag  contians a 1 or zero.

jnc printSpace            ;if carry flag contains a zero,jump to printSpace


printDollar:            ;if carry flag contains a 1,print dollar sign
mov dx,'$'            ;move dollar character into dx and print character
call Display2
dec cl             ;decrement cl and jump to Print
jmp Print


printSpace:           ;if carry flag contains a zero,print space character
mov dx,' '           ;move space character into dx and print character
call Display2
dec cl            ;decrement cl and jump to Print
jmp Print

        Next:            ;move to next byte in the array
mov dx,cr_lf     ;move cursor to beginning of next line
call Display

inc bx            ;increment bx to calculate the index of next byte in the array
mov si,[arrnum5+(bx*1)]     ;calculating index of next byte and store in si

inc ch    ;increment ch to track the number of bytes in the array
cmp ch,6
jle Print_num    ;while ch is less or equal than the array size,jump to Print_num
                                                   ; to read next byte in array
ret

main:
       Output:
mov dx,message                  ;move message into dx register
call Display                         ;display message to ask user input
call Input                         ;save the user input in the al register
mov dl,al                          ;move the input to the dl 8 bit register

push dx                      ;move dx register to stack,which contains the user input

        ;next line
mov dx,cr_lf                  ;dx register is empty,hence move cr_lf into
        call Display                                          ; register to  display blank line


pop dx              ;pop user input from stack back into dx register
sub dl,30h                      ;convert user input character to integer


line_loop: 
cmp dl,5                       ;check if user input is 5 than jump to printno
je printno
           
jmp Output                                        ;if user input is not 5,than ask for user input again

printno:
mov bx,0                       ;set bx to zero,to calculate the next index in array
mov ch,0                       ;set ch to 0 to count bytes in array
mov si,0                       ;initialize si to zero

mov si,[arrnum5]               ;si points to the first index in the array...arrnum5[0]

call Print_num                       ;call function Print_num

jmp Done
Done:
int 20h
« Last Edit: March 25, 2017, 05:58:18 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Nasm - Incorrect output
« Reply #1 on: March 25, 2017, 06:54:10 PM »
Hi ricky008. Welcome to the Forum. I went so far as to edit your post and put the code within "code tags" - just the word "code" in square brackets at the top of your code and "/code" at the end.  (not angle brackets like html but square brackets like a Nasm memory reference). This allegedly makes it easier to read and definitely makes it easier to cut-and-paste.  I'm not in the mood to reboot to DOS right now, so I haven't done it...

I think I see a problem. The si register is 16 bits. That means that when you load it from a byte array, you get two bytes, not one. The first byte goes in the lower 8 bits and the second byte in the high 8 bits. When you rotate si 8 times you're getting the high 8 bits. The low 8 bits are never used. You're running short of 8-bit registers. I would try swapping the usage of bx and si. Use si as your index register, and use bl to hold the byte and rotate it. I think that'll solve the problem, or at least help.

Best,
Frank


Offline ricky008

  • Jr. Member
  • *
  • Posts: 2
Re: Nasm - Incorrect output
« Reply #2 on: March 27, 2017, 05:22:48 PM »
HI,Frank
You are awesome,I followed your advise and the program runs perfectly now.
Thank you very much!!!!