How to show the result in 3 decimal places in masm 8086?

23 Views Asked by At

.model small .stack 100h .data
weight DW 9000 ; Multiplied by 100 to handle up to 2 decimal places service_rate DW 5 ; Stored as 5 (for 5%)

service_charge DW ?
total_price DW ?
total_price_str db 9 dup(?), '$' ; Allocate space for the string representation of total_price

.code main proc mov ax, @data mov ds, ax

;CALCULATE THE SUB PRICE
MOV AX, WEIGHT

; Calculate service_charge = price * service_rate / 100
IMUL service_rate
MOV BX, 100
IDIV BX
MOV SERVICE_CHARGE, AX

; Calculate total_price = (price - service_charge) * WEIGHT 
MOV AX, weight
SUB AX, SERVICE_CHARGE
MOV total_price, AX

; Convert total_price to string
LEA DI, total_price_str
MOV CX, 0
MOV AX, total_price
convert_to_string:
    MOV DX, 0
    MOV BX, 10
    IDIV BX
    ADD DX, '0'
    PUSH DX
    INC CX
    CMP AX, 0
JNE convert_to_string
pop_characters:
    POP DX
    MOV [DI], DL
    INC DI
    ; Insert decimal point after 4 characters
    CMP CX, 3
    JNE no_decimal
    MOV BYTE PTR [DI], '.'
    INC DI
no_decimal:
    LOOP pop_characters
MOV BYTE PTR [DI], '$'

; Print total_price_str
MOV AH, 09H
LEA DX, total_price_str
INT 21H

; End program (assuming DOS interrupt)
MOV AX, 4C00H
INT 21H

main endp end main

I able to calculate 2 decimal places but I cannot calculate for the 3 decimal places

0

There are 0 best solutions below