Enter a signed decimal digit string and output the value of the string in hexadecimal format.

Source: Internet
Author: User

Data Segment
Buf1 dB 20
DB?
DB 20 DUP (?)
Buf2 dB 20
DB?
DB 20 DUP (?)
Input dB 'Remember to use the symbol, input: $'
Output dB 'output: $'
CTRL dB 0dh, 0ah, '$'
Shi DW 0ah; it cannot be dB, DW is used, and word multiplication is required
Fuhao DB? ; Save the symbol
Iint DW 0
Data ends

Stack segment
DB 200 DUP (0)
Stack ends

Code segment
Assume Cs: code, DS: data, SS: Stack
 
Start:
MoV ax, Data
MoV ds, ax
; Output prompt 'input :'
Lea dx, input
MoV ah, 9
Int 21 h
; Input value
Lea dx, buf1
MoV ah, 10
Int 21 h
; Output carriage return line feed
Lea dx, CTRL
MoV ah, 9
Int 21 h
Convert ASCII code into decimal

MoV Cl, buf1 + 1; get the number of characters
Lea Si, buf1 + 2; first address
MoV Al, [Si]
MoV [fuhao], Al
; Converted to iint
Because of mul DL, the first character is processed separately.
INC Si; starting with the first non-sign
Dec Cl; Do not forget this step.
MoV BX, 0
MoV DL, [Si]
MoV DH, 0
MoV ax, DX
Sub Al, 30 h
Dec Cl
INC Si
Next:
; MoV DL, 10; if the two rows are used, if it is a word multiplication, the result will exist in Dx and ax. Therefore, when the hexadecimal value is 4 bits, the high part of ax is in DL, so it is always wrong.
; Mul 10
Mul [Shi]
Add ah, DL
MoV DL, [Si]
MoV BL, DL
Sub BL, 30 h
Add ax, BX
Dec Cl
INC Si
CMP Cl, 0
JNE next
MoV [iint], ax

CMP [fuhao], 02bh; positive symbol
Je Zheng
; Negative symbol bit
MoV BX, 0 ffffh
Sub BX, [iint]
Add Bx, 01 H
MoV [iint], BX
Zheng:
Convert decimal to hexadecimal
MoV Cl, 0; record the number of data entries into the stack
MoV ax, [iint]
MoV dx, 0
To:
MoV BX, 16
Div BX; ax/(OPS) ==> Al, ah
MoV BX, ax; storage provider, used for Division
Push DX; Add the remainder to the stack, so assign the operator 0
INC Cl
MoV ax, BX; restoration provider, used for synchronization and Division
MoV dx, 0; eliminate the remainder of the last time
CMP ax, 0
JNE

; Processing bits
MoV BL, 4
Sub BL, Cl
MoV Cl, BL
CMP Cl, 0
Jbe AAAA; if it is less than or equal to 0, it indicates that there are already four digits.
Bu0:
MoV ax, 0 h
PUSH AX
Dec Cl
CMP Cl, 0
JNE bu0

Aaaa:
; Outbound Stack
MoV Si, 0
MoV Cl, 0
Outstack:
Pop ax
CMP Al, 9; Convert hexadecimal to ASCII
Ja dayu9
Add Al, 30 h
JMP xiaoyu9
Dayu9:
Add Al, 37 h
Xiaoyu9:
MoV [Si] [buf2], Al
INC Si
INC Cl
CMP cl, 4
JNE outstack
MoV [Si] [buf2], 'H'
; Output prompt
Lea dx, output
MoV ah, 9
Int 21 h

; Output Value
Lea Si, buf2
MoV Cl, 5
Print:
MoV dx, [Si]
MoV ah, 2
Int 21 h
INC Si
Dec Cl
Jnz print

MoV ah, 4ch
Int 21 h
Code ends
End start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.