1

I'm doing a loop to display numbers from 0 to 9. I can't figure how to write on the next line.

I'm doing this to try to understand asm, but I can't find any documentation I can understand.

section .text
  global _start

_start:
  mov ecx, 10
  mov eax, '0'

l1:
  mov [num], eax
  mov eax, 4
  mov ebx, 1
  push rcx

  mov ecx, num
  mov edx, 1
  int 0x80
  mov eax, [num]
  sub eax, '0'
  inc eax
  add eax, '0'
  pop rcx
  loop l1
  mov eax,1
  int 0x80
section .bss
num resb 1

The output is "0123456789" on one line, I'd like each number on a different line.

Jester
  • 56,577
  • 4
  • 81
  • 125
LCLMS
  • 23
  • 3
  • 7
    So print a line feed in between, same as in any other language. PS: don't use the `int 0x80` in 64 bit code. See [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730/547981) – Jester Sep 05 '19 at 15:30

1 Answers1

2
  mov eax, '0'
l1:
  mov [num], eax

Your num resb 1 directive reserves 1 byte but your mov [num], eax instruction writes 4 bytes. Not to much of a problem here but definitely not a good habbit!

mov eax, [num]
sub eax, '0'
inc eax
add eax, '0'

You can move to the next character without converting from character to number and back.

mov eax, [num]
inc eax

And even simpler if you write:

inc byte [num]

To solve the 'write on another line' issue.

Add a data section and suffix the "0" character by a linefeed (10). Don't forget to set the count in EDX to 2.

section .data
  num db "0", 10

section .text
  global _start

_start:
l1:
  mov eax, 4
  mov ebx, 1
  mov ecx, num
  mov edx, 2
  int 0x80
  inc byte [num]
  cmp byte [num], "9"
  jbe l1
Fifoernik
  • 9,779
  • 1
  • 21
  • 27