:$Hi! Please excuse me if this is a stupid piece of programming code I wrote. I just started to learn how to program in x86 assmbly and apparently it's gonna be quite a while before I get the hang of it.
Well with this program I want to convert the value in AX into an integer and display it on the screen. I tried to define ten byte variables to represent each bit of the integer and add the value of AX to them "one" at a time. I know this is not the best and usual way to convert something to decimal but the idea just poped out and I wanted to implement it in a program. I haven't the slightest clue why it doesn't work so could you please kindly take a look at it and help me out? Thank you very much! Here's the code:

.8086
DATA SEGMENT
STORY DB 10 DUP(30H)
DATA ENDS
;-------------------------------------
STACK SEGMENT STACK
DB 100 DUP(?)
STACK ENDS
;-------------------------------------
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STACK
MOV AX, DATA
MOV DS, AX
MOV AX, STACK
MOV SS, AX
;-------------------------------------
MAIN PROC FAR
MOV AX, 3
CV_START:
SUB BP, BP
DEC AX
CV_CARRY:
INC STORY[BP]
CMP STORY[BP], 3AH
JNE CV_NEXT
MOV STORY[BP], 30H
INC BP
JMP CV_CARRY
CV_NEXT:
CMP AX, 0
JE OUTPUT
JNE CV_START
;--------------------------------------------

OUTPUT:
MOV BP, 9
MOV CX, 10
OUT_LOOP:
MOV DL, STORY[BP]
MOV AH, 02
INT 21H
LOOP OUT_LOOP

LEAVE:
MOV AH, 4CH
INT 21H

MAIN ENDP
CODE ENDS
END MAIN

Converting binary ordinal to ASCI decimal representation.
----------------------------------------------
Memory locations contain values, in order to display
them we must convert the value to the digits of a
number system and display a character for each one.

For example to convert the value 123d into '123' 0x31,0x32,0x33:
The steps are taken:
123 / 100 = 1
023 / 010 = 2
003 / 001 = 3

Third digit:
123 / 100 = 1 + 0x30 = 0x31 '1'

100 * 1 = 100 and 123 - 100 = 23
100 / 10 = 10 new divisor

Second digit:
23 / 10 = 2 + 0x30 = 0x32 '2'

10 * 2 = 20 and 23 - 20 = 3
10 / 10 = 1 new divisor

First digit:
3 / 1 = 3 + 0x30 = 0x33 '3'

Understanding The DIVide instruction:
The DIV instruction divides a 16-bit dividend in AX
by an 8-bit divisor, remainder is returned in AH and
quotient in AL.
Or a 32-bit dividend in DX:AX by a 16-bit divisor,
remainder is returned in DX and quotient in AX.
The divisor is the operand.

For example:

; 16 / 4
mov ax, 0x10
mov bl, 0x4
div bl 
; after execution AL=4 AH=0

Edited 7 Years Ago by NotNull: n/a

If you wanted to display each bit of a value you
could use the test instruction:

jmp start
var db 0x3

start:
mov bx, offset var
mov al, 4   ; bit 2, 2^2=4
call testbit 
mov al, 2   ; bit 1, 2^1=2
call testbit
mov al, 1   ; bit 0, 2^0=1
call testbit
int 0x20

testbit:
test byte [bx], al
jz testbit_clear
mov dl, 0x31
mov ah, 0x2
int 0x21
jmp testbit_e
testbit_clear:
mov dl, 0x30
mov ah, 0x2
int 0x21
testbit_e:
ret

prints '011'

Thank you so much! Those were expanations with patience! I learned a lot from that! They were such a help! Thank you again!

This article has been dead for over six months. Start a new discussion instead.