1

I am trying to multiply two integers and then converting them to ascii and then printing. The code compiles fine and no errors are given but nothing shows on screen.

section .text

global _start

_start:
    ;to print a number after multiplication
    mov eax, x
    mov ebx, y
    MUL ebx

    ADD eax, '0' ;convert product to ascii
    mov [mynum], eax
    
    mov     ecx, mynum
    mov     edx, 1
    mov     ebx, 1
    mov     eax, 4
    int     0x80
    
    mov eax, 1
    mov ebx, 0
    int 0x80

section .data
    x dd 2
    y dd 3

segment .bss
    mynum resd 1

Thank you for helping!

EDIT: When I remove the interrupt on line 10, a zero is now printed to the terminal

  • What do you expect these various `int 0x80` instructions to do? This instruction performs a system call, but I don't really see what system calls you attempt to perform. Perhaps [edit] your post and comment each instruction with what you expect it to do. – fuz Jun 12 '22 at 00:49
  • @fuz After re-reading the code, to my understanding the ```int 0x80``` on lines 10 and 14 are useless since we are not calling a specific system call like the write or exit call. That's why we would need them on lines 20 and 24. I have deleted the two useless ones. – Sebastian Bucarion Jun 12 '22 at 01:00
  • They are not just useless, they like cause the problem you observe. Every `int 0x80` instruction does a system call. But if you haven't set up to do a system call, the results will be hard to predict and may cause a crash. So don't just sprinkle `int 0x80` in your code whenever you feel like it. – fuz Jun 12 '22 at 01:01
  • 2
    The remaining problem is that `mov eax, x` and `mov `ebx, y` load the addresses of the labels into `eax` and `ebx`. You're missing two pairs of brackets. – fuz Jun 12 '22 at 01:02
  • Single-step your code in a debugger to see what values are in registers. And use `strace ./a.out` to see what system calls you make. Your simple int->ASCII conversion only works for single-digit numbers, so there's no point telling `write` to copy 4 bytes to stdout. You're outputting 3 bytes of binary zeros, as you could see if you pipe into `hexdump -C` or use `strace`. Also, if you don't want the high half of the product (in EDX), you should just use `imul eax, [y]` – Peter Cordes Jun 12 '22 at 01:08
  • 2
    Yep, I did not fully understand the purpose of ```int 0x80``` so thank you. And yes adding the brackets fix my code because I multiply the actual value instead of the address. Thank you fuz. – Sebastian Bucarion Jun 12 '22 at 01:09

0 Answers0