0

I'm new to x86 assembly. My output currently looks like this:

1|*
2|** ...
8|********
9|*********
10|**********
11|***********

I want there to be a space in 1-9 so that it forms a straight line. Like this:

 9|
10|
11|

I don't know how to do it. I was thinking maybe using printChar (I want it in x86 assembly)

My code for printing the stars and lines:

beginLoop:   dec numItems
         push numItems
         call printInt //Prints 'numItems' number

         mov al,'|'
         push al
         call printChar //Prints bar line next to number  

  starLoop:    mov al, '*'
               push al
               call printChar //Prints a single star

         mov [numStars], 0 
         inc numStars  //numStars increases by 1
         mov ecx, [esi] //ebx knows inputted value 
         cmp [numStars], ecx //numStars must equal value inputted 
         jne starLoop

nextValue : call printNewLine
            add esi, 4 //Move to next number
            cmp[esi], [items]
            jne beginLoop
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Susmi Benny
  • 1
  • 1
  • 2
  • 1
    If you can output characters like `9`, `|`, `.` or `*`, you can output a space too. Just output an ASCII 32. What is the problem? – Rudy Velthuis Oct 22 '17 at 20:11
  • I tried it but it didn't do anything. I am using visual studio. I don't know whether that may be it. – Susmi Benny Oct 22 '17 at 20:12
  • My code was : { mov al,'|' push al call printChar } – Susmi Benny Oct 22 '17 at 20:13
  • Then show how you output, say, a `*`. You simply output a space the same way. Edit your question and show your relevant code. – Rudy Velthuis Oct 22 '17 at 20:13
  • `push al` is an invalid instruction. `mov al,'|'` is not referring to the space character. Use ASCII code 32 for that. Write `mov al, 32` or clearer `mov al, ' '`. – Sep Roland Oct 22 '17 at 20:17
  • 2
    For the record, [MSVC assembles `push al` into a `push eax` instruction](https://godbolt.org/g/GMCcro). This is pretty broken (and seems like a terrible design vs. being an assemble-time error), but happens to work for what the OP is doing. I assume MASM behaves the same as MSVC inline asm for this. (`push al` is not encodeable in machine-code. The only possible operand-sizes are 32 and 16. (Or 64 and 16 in 64-bit code.) https://stackoverflow.com/questions/45127993/how-many-bytes-does-the-push-instruction-pushes-onto-the-stack-when-i-dont-spec) – Peter Cordes Oct 22 '17 at 20:49
  • 1
    @PeterCordes See, I didn't know there are assemblers that allow `push al`. IMO that should never exist. I've always preferred (and still do) WYSIWYG. – Sep Roland Oct 22 '17 at 21:02
  • Agreed. I got curious because this user posted a question a day or so ago with the same invalid instruction and claimed it assembled, so I tested it. Assembling `push al` is just confusing. – Peter Cordes Oct 22 '17 at 21:07

1 Answers1

0

Your code now will have something like this:

mov     eax, [number]
call    YourPrintRoutine

To introduce right-alignment on these numbers, you compare the number with 10, and only when the number is less than 10 do you first print a single space character.

    mov     eax, [number]
    push    eax
    cmp     eax, 10
    jnb     NotBelow10
    push    " "
    call    PrintChar
NotBelow10:
    call    PrintInt

Are you sure this is the code that produces the above results?

  • With mov [numStars], 0 in the middle of your starLoop it's a miracle that it can display the right amount of stars!
  • With dec numItems decrementing a value it's strange that you get an incrementing sequence of numbers on screen!
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • I have tried your solution and it still isn't working. – Susmi Benny Oct 22 '17 at 20:22
  • You have `mov eax, " "` *after* your `push eax`. Surely you should just `push " "` if the function uses a stack-args calling convention. (I don't think it's Irvine32 or whatever with args in eax) – Peter Cordes Oct 22 '17 at 20:23
  • Apparantly you push the argument for *printInt* instead of passing it in `EAX`. Secondly to display the spacecharacter, you don't use *printInt*, but a text print service. – Sep Roland Oct 22 '17 at 20:26