| |

VerySource

 Forgot password?
 Register
Search
View: 3423|Reply: 11

Assembler for converting binary to decimal

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 10:00:01
| Show all posts |Read mode
Assembler for converting binary to decimal
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Canada

Post time: 2020-12-9 18:00:02
| Show all posts
; BIN2DEC.ASM-Converts a 16-bit binary value into four decimal digits.
; Input: The binary value in the pair (R1,R0).
; Output: Four decimal digits in R13 to R10, the most significant in R13.
; Algorithm:
; R13 = 0; Increments R13 while (R1,R0)-1000 is positive
; R12 = 0; Increments R12 while (R1,R0)-100 is positive
; R11 = 0; Increments R11 while (R1,R0)-10 is positive
; R10 = R0;

0x0000: MODEL A; Processor uP1232a

; For example: 0x2694 = 9.876
LD R1, 0x26
LD R0, 0x94

DEC2BIN:
LD R13, 0
LD R12, 0
LD R11, 0
LD R10, 0

LD R3, 0x03
LD R2, 0xE8; 0x03E8 = 1.000
LOOP1: INC R13
SUB R0, R2
SBC R1, R3; R1R0-1000
JP NC, LOOP1
DEC R13
ADD R0, R2
ADC R1, R3

LD R3, 0
LD R2, 100
LOOP2: INC R12
SUB R0, R2
SBC R1, R3; R1R0-100
JP NC, LOOP2
DEC R12
ADD R0, R2
ADC R1, R3

LD R3, 0
LD R2, 10
LOOP3: INC R11
SUB R0, R2
SBC R1, R3; R1R0-10
JP NC, LOOP3
DEC R11
ADD R0, R2
ADC R1, R3

LD R10, R0

#align; Aligns code
HALT: BREAK; Break-point
GOTO HALT; Stop processing until RESET

; End of file
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 18:45:01
| Show all posts
8086 assembly

btod2 is a process. Before calling the process, the binary number to be converted is placed in the BX register. disp2 is a process of displaying decimal numbers.

btod2 proc
push ax
push bx
push cx
push dx
mov cx,10000
call disp2
mov cx,1000
call disp2
mov cx,100
call disp2
mov cx,10
call disp2
mov cx,1
call disp2
pop dx
pop cx
pop bx
pop ax
ret
btod2 endp
disp2 proc; a function to display one digit
mov dl,0
again1: cmp bx,cx
jb ext1
inc dl
sub bx,cx
jmp again1
ext1: add dl,30h
cmp dl,'0'
mov ah,02h
int 21h
ret
disp2 endp
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 19:00:01
| Show all posts
Divide more than ten
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 19:15:01
| Show all posts
ttyy0785, what is your compilation based on? ?

After watching for a long time, it doesn’t look like ARM, not like C51, and not even X86 series~~~~~

Enlighten me~~~~~~Thank you~!
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-9 19:30:01
| Show all posts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
; Subroutine to input a decimal data from the keyboard and convert it to binary
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
    P_GETNEW PROC
              PUSH BX; Export parameter: AX=Binary number represented by complement
              PUSH CX; Note: Use "-" to guide negative numbers, and the data range is +32767~-32768
              PUSH DX
              XOR BX, BX; BX saves the result
              XOR CX, CX; CX is positive and negative sign, 0 is positive, -1 is negative
              MOV AH,1; enter a character
              INT 21H
              CMP AL,'+'; is "+", continue to input characters
              JZ READ1
              CMP AL,'-'; Yes "-, set the -1 flag
              JNZ READ2
              MOV CX,-1
                                            
    READ1: MOV AH,1; continue to input characters
              INT 21H
              
    READ2: CMP AL,'0'; if it is not a character between 0-9, the input data is over
              JB READ3
              CMP AL,'9'
              JA READ3
              SUB AL, 30H; is a character between 0-9, then converted to a binary number
              ; Use the shift instruction to multiply the value by 10: BX←BX*10
              SHL BX,1
              MOV DX,BX
              SHL BX,1
              SHL BX,1
              ADD BX,DX
              
              MOV AH,0
              ADD BX,AX; After the input value is multiplied by 10, it is added to the newly input value
              JMP READ1; continue to enter characters
              
    READ3: CMP CX,0; It is a negative number, to complement
              JZ READ4
              NEG BX
              
    READ4: MOV AX,BX; set export parameters
              POP DX
              POP CX
              POP BX
              RET; subroutine return
    P_GETNEW ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
; Output subroutine to convert binary data to decimal
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
P_OUTPUT PROC
              PUSH AX; Entry parameter: shared variable WTEMP
              PUSH BX
              PUSH DX
              MOV AX, WTEMP; Take out the displayed data
              TEST AX, AX; Determine whether the data is zero, positive or negative
              JNZ WRITE1
              MOV DL,'0'; is '0', exit after displaying '0'
              MOV AH,2
              INT 21H
              JMP WRITE5
  
    WRITE1: JNS WRITE2; is a negative number, display "-"
              MOV BX, AX; AX data is temporarily stored in BX
              MOV DL,'-'
              MOV AH,2
              INT 21H
              MOV AX,BX
              NEG AX; Data complement (absolute value)
              
    WRITE2: MOV BX,10
              PUSH BX; 10 is pushed onto the stack as a push exit flag
              
    WRITE3: CMP AX,0; data (quotient) is zero, turn to display
              JZ WRITE4
              SUB DX,DX; expand the dividend DX.AX
              DIV BX; data divided by 10
              ADD DL,30H; The remainder (0~9) is converted to ASCⅡ code
              PUSH DX; Every bit of data is pushed into the stack first low bit and then high bit
              JMP WRITE3
              
    WRITE4: POP DX; Every bit of data pops out of the stack first high and then low
              CMP DL,10; is the end mark 10, then exit
              JE WRITE5
              
              MOV AH,2; display
              INT 21H
              JMP WRITE4
              
    WRITE5: POP DX
              POP BX
              POP AX
               MOV DL,20H
              MOV AH,02H
              INT 21H
              RET; subroutine return
   P_OUTPUT ENDP
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-9 20:00:01
| Show all posts
That is MIPS compilation. :)
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 21:45:01
| Show all posts
[code=Assembly]data segment
num db 01Fh
data ends
To
stack segment STACK
db 256 dup(0)
tos label word
stack ends
code segment
assume cs:code,ds:data
begin:
        mov ax,0d
        mov ax,0dh
mov ax,data
mov ds,ax
mov es,ax
To
mov ax,stack
mov ss,ax
mov sp,offset tos
To
xor ax,ax
mov al,num
xor cx,cx
; The following is to display the number in cout in decimal
DoDiv:
mov bl,10
div bl; quotient of al←ax/10
                   ;ah← the remainder of ax/10
push ax; save the result
inc cx
xor ah, ah; ah clear, clear the remainder
cmp al,0; Determine whether the quotient is zero
jnz DoDiv
To
DoPrt:
pop dx; dx saves the result of each division
xchg dh,dl
add dl,30h; the number in dl is the quotient after each division, which is expressed as ascii
mov ah,2
int 21h
loop DoPrt
To
exit:
mov ax,4c00h; exit DOS
int 21h
code ends
end begin [/code]
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 22:00:02
| Show all posts
The previous one didn't understand what it meant. ,. . .
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-9 22:15:01
| Show all posts
My program is to display num db 01Fh in decimal
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list