Hello everyone,

I'm a student who's new to assembly language and I'm running into some trouble with an assignment.  The program reads in a 4 digit number from the keyboard (using direct addressing), performs some arithmetic and conversion, and then displays the answer.  For the first version of the program the output was to be a 4 digit number with any necessary leading 0's (ie with input of 2004 the result is 0043).  The version I'm working on now uses loop and jump commands and indirect addressing for the input, converting the input characters from hex to a decimal number, converting the answer from decimal to hex characters, and then to remove the leading 0's.

I'm running into two problems with the second version.  The first problem is that my answer now gets printed with spaces in-between each number, 0 0 4 3, instead of 0043.  And the second problem is that my loop to remove the leading zeros isn't working.  To remove the zeros I'm trying to replace them with spaces by comparing the first digit to zero and if it's equal to zero, replacing it with the ASCII value 20h.  The way I'm trying to compare the values doesn't seem to be working.

If anyone has any ideas as to what I'm doing wrong it would be greatly appreciated.  I've been staring at this for hours and can't seem to figure out what's wrong.

I'm using MASM 6.11 and Microsoft Linker 5.63.  I attached my source code to this post.
Attachments:
Posted on 2008-11-14 17:59:44 by Unclejunebug
Here's the code not in an attachment....

.model small
.stack 100h
.data


count DW 0
year DW 0
input DB ?,?,?,?

prompt DB "Enter a four digit number for year: ",0dh,0ah,'$'

space DB 0dh,0ah
msg DB "The result is: "
r2d2 DB ?,?,?,?
DB 0dh,0ah,'$'

.code
main proc

mov ax,@data
mov ds,ax

;display prompt
mov ah,9
mov dx,offset prompt
int 21h

mov si,offset input

;enter values for input
L1: mov ah,01h
int 21h
CMP al,0dh
JE L2
mov ,al
inc si
inc count
Loop L1

L2: mov cx,count
mov si,offset input
mov bx,10
mov ax,0
mov dx,0

;convert input to decimal
L3:    mul bx
add al,
sub al,30h
inc si
Loop L3

mov year,ax
inc year

;perform calculation
mov ax,year
mov bx,3
mul bx
mov bx,7
div bx
mov ax,0
add ax,36
add ax,year
sub ax,2000
add ax,dx

mov cx,count
mov bl,10
mov si,offset r2d2
add si,count
sub si,1

;convert answer to decimal
L4:    mov dx,0
div bx
add dx,30h
mov ,dx
dec si
Loop L4

mov si,offset r2d2

;remove leading zeros
L5:    mov ax,
CMP ax,0
JG L6
mov ,21h
inc si
JMP L5


;display result
L6: mov ah,9
mov dx,offset space
int 21h

mov ax,4c00h
int 21h

main endp
end main
Posted on 2008-11-14 22:01:47 by Unclejunebug
ok heres some help regarding the removing of leading zeroes..
the hex value for zero is 30h, not zero.. and the hex value for space is 20h (32d), not 21h.

That should get you moving again :)
Posted on 2008-11-30 03:29:51 by Homer
Posted on 2008-11-30 12:44:39 by SpooK
;remove leading zeros
L5:    mov ax,


Furthermore, always make sure you use the correct register size for the task at hand.

Here, the task at hand is to check each byte of the string for the presence of a leading "0". In order to accomplish that, the instruction should be:

mov al,

Remember that you can use characters within quotation marks instead of their actual ascii values. It's also less prone to errors. Examples:

cmp al,"0"
mov byte ptr," "
Posted on 2008-11-30 19:39:08 by Raymond