Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7658949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:16:06+00:00 2026-05-31T13:16:06+00:00

I’m trying to convert a string I read with this code to binary and

  • 0

I’m trying to convert a string I read with this code to binary and hexa.

READ_STRING:
    MOV DX, offset buffer
    MOV AH, 0Ah
    INT 21h
    MOV SI, 1d
    MOV AX, 0
    XOR CX, CX
    MOV CL, buffer[SI]
    INC SI

LOOP_1:
    MOV DX, 10
    MUL DX
    MOV DL, buffer[SI]
    SUB DL, 30h
    MOV DH, 0
    ADD AX, DX
    INC SI
    LOOP LOOP_1

    RET

So far I have this code for binary output but it always prints “1001” (9 in decimal):

NEXT:

    XOR AX, AX
    XOR BX, BX
    XOR CX, CX
    MOV CL, 2
    MOV AL, byte ptr[nombre]
    MOV DI, offset binaire

; DIV : divide AX by CL. Remainder in AH and result in AL

LOOP:
    DIV CL ; remainder in AH, quotient in AL
    ADD AH, '0' ;  0 -> '0' , 1 -> '1'
    MOV [DI], AH ; Saves the remainder in the array
    INC DI
    MOV AH, 0 ; reset AH for next division
    CMP AL, 0 ; if result is 0, end
    JNE LOOP

;Prints the binary number               
    MOV DX, offset binaire
    CALL WRITE_STRING

Thanks! If you need anything else just ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:16:06+00:00Added an answer on May 31, 2026 at 1:16 pm

    Before worrying about whether or not you can display the value as binary or hexadecimal; check to make sure your code correctly converts the user’s input into an integer (e.g. with a debugger).

    For binary, consider something like this:

        mov bx,ax              ;bx = the value to display as binary
        mov cx,16              ;cx = number of bits to display
        mov di,offset binaire  ;es:di = address to store string
    
    .nextBit:
        xor ax,ax              ;al = 0
        add bx,bx              ;bx = value * 2; carry flag = overflow
        adc al,0               ;al = '0' or '1'
        stosb                  ;Add new character to string
        loop .nextBit
        mov byte [di],0        ;Terminate the string (ASCIIZ?)
    
        mov dx, offset binaire
        call WRITE_STRING
    

    For hexadecimal, it’s the same basic idea, except you need to extract the highest 4 bits:

        mov bx,ax              ;bx = the value to display as binary
        mov cx,4               ;cx = number of nibbles to display
        mov di,offset binaire  ;es:di = address to store string
    
    .nextNibble:
        mov ax,bx              ;ax = value
        shr ax,12              ;ax = highest 4 bits of value
        shl bx,4               ;bx = value << 4
        add al,'0'
        cmp al,'9'
        jbe .gotChar
        add al,'A' - '9'
    .gotChar:
        stosb                  ;Add new character to string
        loop .nextBit
        mov byte [di],0        ;Terminate the string (ASCIIZ?)
    
        mov dx, offset binaire
        call WRITE_STRING
    

    Note 1: I haven’t tested any of the code above, and I normally use NASM (not MASM), so it may not assemble “as is”.

    Note 2: Example code above is intentionally simple. For performance you could do a lot better using lookup tables instead.

    Note 3: These aren’t complex algorithms and you shouldn’t need to mess about with high level languages first (unless you don’t understand the theory/maths behind binary/hex conversion perhaps). Also, an algorithm that seems elegant in one language can be an ugly mess in another language (e.g. you can’t detect overflow in an easy/clean way in C, so the method used for binary conversion above wouldn’t be obvious or elegant in C; and a different method that is elegant in C might suck badly in assembly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.