Generate a string representing the decimal value of a given binary numberAdd 2 numbers and print the result using Assembly x86print number to screen assemblyPrint out register in decimal without printfNASM Guessing Number Game Gone WrongHelp on VGA and putpixel intel x86 asm AT&T syntaxBase pointer and stack pointerHow to get window proc parameters?Calling C function from assembly, segfaultAssembly code for converting hex to dec is not workingFloating point exceptions in x86 NASM assembly using div instruction(VC++) Runtime Check for Uninitialized Variables: How is the test Implemented?NASM on linux: Using sys_read adds extra line at the endSafely Searching Virtual Address Space using NASM

Is a request to book a business flight ticket for a graduate student an unreasonable one?

Elf (adjective) vs. Elvish vs. Elven

WTB Horizon 47c - small crack in the middle of the tire

How do native German speakers usually express skepticism (using even) about a premise?

Why do you use the "park" gear to park a car and not only the handbrake?

When did "&" stop being taught alongside the alphabet?

Why do we need common sense in AI?

Credit score and financing new car

Through: how to use it with subtraction of functions?

Why archangel Michael didn't save Jesus when he was crucified?

Addressing unnecessary daily meetings with manager?

Why did Old English lose both thorn and eth?

Is there any reason why MCU changed the Snap to Blip

How can a dictatorship government be beneficial to a dictator in a post-scarcity society?

How can I effectively communicate to recruiters that a phone call is not possible?

Would a carnivorous diet be able to support a giant worm?

Is it possible to split a vertex?

What is the right approach to quit a job during probation period for a competing offer?

Party going through airport security at separate times?

Is there a nice way to implement a conditional type with default fail case?

How quality assurance engineers test calculations?

Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?

Why different specifications for telescopes and binoculars?

Should I include code in my research paper?



Generate a string representing the decimal value of a given binary number


Add 2 numbers and print the result using Assembly x86print number to screen assemblyPrint out register in decimal without printfNASM Guessing Number Game Gone WrongHelp on VGA and putpixel intel x86 asm AT&T syntaxBase pointer and stack pointerHow to get window proc parameters?Calling C function from assembly, segfaultAssembly code for converting hex to dec is not workingFloating point exceptions in x86 NASM assembly using div instruction(VC++) Runtime Check for Uninitialized Variables: How is the test Implemented?NASM on linux: Using sys_read adds extra line at the endSafely Searching Virtual Address Space using NASM






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm trying to do an assignment linking c and nasm. The C program sends me a string representing a 32-bit number (e.g. "000...0011"). I need to print its value, as a string (the string "3" for the example above), using C's printf with %s.



Note: to make life easier, I'll ignore the case of negative numbers for now.



I'm new to nasm and so I pretty much have no clue what goes wrong where. I tried converting the given string to a number, store it somewhere, and then have it printed, but this simply prints the binary representation.



Here's my code:



.rodata section
format_string: db "%s", 10, 0 ; format string

.bss section
an: resb 12 ; enough to store integer in [-2,147,483,648 (-2^31) : 2,147,483,647 (2^31-1)]
convertor:
push ebp
mov ebp, esp
pushad

mov ecx, dword [ebp+8] ; get function argument (pointer to string)

mov eax, 1 ; initialize eax with 1 - this will serve as a multiplier
mov dword [an], 0 ; initialize an with 0
ecx_To_an:
cmp eax, 0 ; while eax != 0
jz done ; do :
shr dword [ecx], 1 ;
jnc carry_flag_not_set ; if carry isn't set, lsb was 0
add [an], eax ; else - lsb was 1 - an += eax
carry_flag_not_set:
shl eax, 1 ; eax = eax*2
jmp ecx_To_an ; go to the loop
done:
push an ; call printf with 2 arguments -
push format_string ; pointer to str and pointer to format string
call printf


I don't see how it can be possible to print the int value, given that I can't change the %s argument that is given to printf.



Help will be much appreciated.










share|improve this question



















  • 1





    You will need to convert to decimal digits. Plenty of examples for that.

    – Jester
    Mar 26 at 0:50






  • 3





    Sample decimal conversion codes: 1 2 3 You can find plenty more.

    – Jester
    Mar 26 at 1:11






  • 3





    You never increment ecx, so you are only looking at the first 4 bytes of the string.

    – prl
    Mar 26 at 1:11







  • 1





    Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

    – Jester
    Mar 26 at 1:48







  • 1





    Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

    – Peter Cordes
    Mar 26 at 20:10

















0















I'm trying to do an assignment linking c and nasm. The C program sends me a string representing a 32-bit number (e.g. "000...0011"). I need to print its value, as a string (the string "3" for the example above), using C's printf with %s.



Note: to make life easier, I'll ignore the case of negative numbers for now.



I'm new to nasm and so I pretty much have no clue what goes wrong where. I tried converting the given string to a number, store it somewhere, and then have it printed, but this simply prints the binary representation.



Here's my code:



.rodata section
format_string: db "%s", 10, 0 ; format string

.bss section
an: resb 12 ; enough to store integer in [-2,147,483,648 (-2^31) : 2,147,483,647 (2^31-1)]
convertor:
push ebp
mov ebp, esp
pushad

mov ecx, dword [ebp+8] ; get function argument (pointer to string)

mov eax, 1 ; initialize eax with 1 - this will serve as a multiplier
mov dword [an], 0 ; initialize an with 0
ecx_To_an:
cmp eax, 0 ; while eax != 0
jz done ; do :
shr dword [ecx], 1 ;
jnc carry_flag_not_set ; if carry isn't set, lsb was 0
add [an], eax ; else - lsb was 1 - an += eax
carry_flag_not_set:
shl eax, 1 ; eax = eax*2
jmp ecx_To_an ; go to the loop
done:
push an ; call printf with 2 arguments -
push format_string ; pointer to str and pointer to format string
call printf


I don't see how it can be possible to print the int value, given that I can't change the %s argument that is given to printf.



Help will be much appreciated.










share|improve this question



















  • 1





    You will need to convert to decimal digits. Plenty of examples for that.

    – Jester
    Mar 26 at 0:50






  • 3





    Sample decimal conversion codes: 1 2 3 You can find plenty more.

    – Jester
    Mar 26 at 1:11






  • 3





    You never increment ecx, so you are only looking at the first 4 bytes of the string.

    – prl
    Mar 26 at 1:11







  • 1





    Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

    – Jester
    Mar 26 at 1:48







  • 1





    Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

    – Peter Cordes
    Mar 26 at 20:10













0












0








0








I'm trying to do an assignment linking c and nasm. The C program sends me a string representing a 32-bit number (e.g. "000...0011"). I need to print its value, as a string (the string "3" for the example above), using C's printf with %s.



Note: to make life easier, I'll ignore the case of negative numbers for now.



I'm new to nasm and so I pretty much have no clue what goes wrong where. I tried converting the given string to a number, store it somewhere, and then have it printed, but this simply prints the binary representation.



Here's my code:



.rodata section
format_string: db "%s", 10, 0 ; format string

.bss section
an: resb 12 ; enough to store integer in [-2,147,483,648 (-2^31) : 2,147,483,647 (2^31-1)]
convertor:
push ebp
mov ebp, esp
pushad

mov ecx, dword [ebp+8] ; get function argument (pointer to string)

mov eax, 1 ; initialize eax with 1 - this will serve as a multiplier
mov dword [an], 0 ; initialize an with 0
ecx_To_an:
cmp eax, 0 ; while eax != 0
jz done ; do :
shr dword [ecx], 1 ;
jnc carry_flag_not_set ; if carry isn't set, lsb was 0
add [an], eax ; else - lsb was 1 - an += eax
carry_flag_not_set:
shl eax, 1 ; eax = eax*2
jmp ecx_To_an ; go to the loop
done:
push an ; call printf with 2 arguments -
push format_string ; pointer to str and pointer to format string
call printf


I don't see how it can be possible to print the int value, given that I can't change the %s argument that is given to printf.



Help will be much appreciated.










share|improve this question
















I'm trying to do an assignment linking c and nasm. The C program sends me a string representing a 32-bit number (e.g. "000...0011"). I need to print its value, as a string (the string "3" for the example above), using C's printf with %s.



Note: to make life easier, I'll ignore the case of negative numbers for now.



I'm new to nasm and so I pretty much have no clue what goes wrong where. I tried converting the given string to a number, store it somewhere, and then have it printed, but this simply prints the binary representation.



Here's my code:



.rodata section
format_string: db "%s", 10, 0 ; format string

.bss section
an: resb 12 ; enough to store integer in [-2,147,483,648 (-2^31) : 2,147,483,647 (2^31-1)]
convertor:
push ebp
mov ebp, esp
pushad

mov ecx, dword [ebp+8] ; get function argument (pointer to string)

mov eax, 1 ; initialize eax with 1 - this will serve as a multiplier
mov dword [an], 0 ; initialize an with 0
ecx_To_an:
cmp eax, 0 ; while eax != 0
jz done ; do :
shr dword [ecx], 1 ;
jnc carry_flag_not_set ; if carry isn't set, lsb was 0
add [an], eax ; else - lsb was 1 - an += eax
carry_flag_not_set:
shl eax, 1 ; eax = eax*2
jmp ecx_To_an ; go to the loop
done:
push an ; call printf with 2 arguments -
push format_string ; pointer to str and pointer to format string
call printf


I don't see how it can be possible to print the int value, given that I can't change the %s argument that is given to printf.



Help will be much appreciated.







assembly x86 nasm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 0:50









Jester

47.5k4 gold badges50 silver badges87 bronze badges




47.5k4 gold badges50 silver badges87 bronze badges










asked Mar 26 at 0:49









J. DoeJ. Doe

11 bronze badge




11 bronze badge







  • 1





    You will need to convert to decimal digits. Plenty of examples for that.

    – Jester
    Mar 26 at 0:50






  • 3





    Sample decimal conversion codes: 1 2 3 You can find plenty more.

    – Jester
    Mar 26 at 1:11






  • 3





    You never increment ecx, so you are only looking at the first 4 bytes of the string.

    – prl
    Mar 26 at 1:11







  • 1





    Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

    – Jester
    Mar 26 at 1:48







  • 1





    Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

    – Peter Cordes
    Mar 26 at 20:10












  • 1





    You will need to convert to decimal digits. Plenty of examples for that.

    – Jester
    Mar 26 at 0:50






  • 3





    Sample decimal conversion codes: 1 2 3 You can find plenty more.

    – Jester
    Mar 26 at 1:11






  • 3





    You never increment ecx, so you are only looking at the first 4 bytes of the string.

    – prl
    Mar 26 at 1:11







  • 1





    Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

    – Jester
    Mar 26 at 1:48







  • 1





    Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

    – Peter Cordes
    Mar 26 at 20:10







1




1





You will need to convert to decimal digits. Plenty of examples for that.

– Jester
Mar 26 at 0:50





You will need to convert to decimal digits. Plenty of examples for that.

– Jester
Mar 26 at 0:50




3




3





Sample decimal conversion codes: 1 2 3 You can find plenty more.

– Jester
Mar 26 at 1:11





Sample decimal conversion codes: 1 2 3 You can find plenty more.

– Jester
Mar 26 at 1:11




3




3





You never increment ecx, so you are only looking at the first 4 bytes of the string.

– prl
Mar 26 at 1:11






You never increment ecx, so you are only looking at the first 4 bytes of the string.

– prl
Mar 26 at 1:11





1




1





Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

– Jester
Mar 26 at 1:48






Well, can you write it in C? Obviously without using standard library functions. The logic is just repeated division by 10. PS: the first one I linked literally says in the comments ... "Convert EAX to ASCII and store it onto the stack" and "Pointer to the first ASCII digit" Heck, it even prints the string although using write() not printf("%s", ...) but those are basically equivalent.

– Jester
Mar 26 at 1:48





1




1





Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

– Peter Cordes
Mar 26 at 20:10





Keeping your data in registers is simpler, as well as more efficient. Since string->integer will leave the result in a register, you should keep it there instead of storing to memory. (Also, finding ways to use fewer instructions and/or cheaper instructions is the fun part of writing in asm.) But anyway, I think my answer that I linked about base10string-> integer is pretty understandable, and easy to adapt for base2string -> integer. Then you feed that integer to an integer->base10string function. Jester linked 3 existing Q&As about that part.

– Peter Cordes
Mar 26 at 20:10












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348358%2fgenerate-a-string-representing-the-decimal-value-of-a-given-binary-number%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55348358%2fgenerate-a-string-representing-the-decimal-value-of-a-given-binary-number%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript