0

So I am working on a little code in Assembly and I was wonder how can I print a message to the user the include a variable.

For example:

  • I want to print: "The div of the numbers you entered are: 'variable'"
  • I checked online but couldn't find an answer I would understand.
  • I know how to print a string, but I don't know to to enter the variable into the string so I could print it all together. Thank's for your time.
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Elad Kobi
  • 21
  • 1
  • 2
  • print to where? a 3D printer? – thang Dec 08 '18 at 23:06
  • @thang Hey. print to the cmd screen, where the program is running from the compailer. Thank's – Elad Kobi Dec 08 '18 at 23:49
  • Elad, that's not enough information. @Thang was being facetious, but his point is valid--no one can possibly answer this question without knowing what type of system you're using. You didn't even say what assembly language you're using. – prl Dec 09 '18 at 00:55
  • @prl Hey. All I know is that it's x86 and 8086 cpu. Thank's. – Elad Kobi Dec 09 '18 at 09:33
  • 8086? So are you using one of the emulators like emu8086 or dosbox? Then you can use DOS and BIOS interrupts (int 10h, int 21h) to display characters and ASCII encoded string data. How you convert your "variable" into stream of characters is up to you, depends the "type" of the "variable", where it's stored, where/how do you want to output + which kind of side-product, i.e. if you want to have fully formatted line in your own memory, you must "insert" substring into larger one, involving moving memory content around. If you want just output on screen (may be built from multiple substrings). – Ped7g Dec 09 '18 at 09:41
  • you can check x86 info tag for many resources/guides/examples: https://stackoverflow.com/tags/x86/info - it would be very helpful if you would point out particular one you "did not understand", and explain which parts you understand and which not, so people can try to explain you the hazy parts. Also printing "variables" and strings and numbers is like next lesson, after you somewhat managed to get the basics like manipulating with bits in registers, so focus first on doing some simple tasks without screen output, just doing some arithmetic, memory manipulation, etc... (use debugger to SEE). – Ped7g Dec 09 '18 at 09:46

1 Answers1

1

I know how to print a string, but I don't know to to enter the variable into the string so I could print it all together

a - Prepare your string so it has an appropriate amount of free space available.

msg     db      'Value is       $'

b - Position an output pointer near the end of the string. In this example it'll be pointing at the $ character.

        lea     di, [msg + 14]

c - Move your variable to the AX register.

        mov     ax, [variable]

d - Call following number to text conversion/insertion routine.

; IN (ax,di)
        mov     bx, 10
More:   xor     dx, dx
        div     bx         ; This divides DX:AX by BX
        dec     di
        add     dl, '0'    ; Turn remainder into a character
        mov     [di], dl   ; Write in string
        test    ax, ax
        jnz     More
        ret

e - Print the whole string at once. You know this already...


For further info read Displaying numbers with DOS

Sep Roland
  • 33,889
  • 7
  • 43
  • 76