0

I want to make simple program that read input from STDIN and then format it in loop and show changed input using stdout. But I get three errors:

 Error: junk `message($counter)' after expression
 Error: suffix or operands invalid for `add'
 Error: suffix or operands invalid for `cmp'

From what I understand from documentation, operands are good for cmp and add, but I'm not 100% sure. I searched internet for some examples and everyone use register as counter. Do I have to use register as counter? I think that probably not, because of ABI convention, but I have no other idea

 SYSEXIT = 1
SYSCALL32 = 0x80
SYSEXIT_SUCCESS = 0
SYSWRITE = 4
STDOUT = 1
SYSREAD = 3
STDIN = 0
message_size = 100

.global _start

.data
message: .space message_size
communicate: .ascii "Podaj slowo:"
communicate_size = . - communicate
counter: .long $0

.text
_start:
mov $SYSWRITE, %eax
mov $STDOUT, %ebx
mov $communicate, %ecx
mov $communicate_size, %edx
int $SYSCALL32

mov $SYSREAD, %eax
mov $STDIN, %ebx
mov $message, %ecx
mov $message_size, %edx
int $SYSCALL32


cipher:
addl $13 message($counter)
incb counter
cmp $counter %eax
ja cipher

mov $SYSWRITE, %eax
mov $STDOUT, %ebx
mov $message, %ecx
mov $message_size, %edx
int $SYSCALL32

mov $SYSEXIT, %eax
mov $SYSEXIT_SUCCESS, %ebx
int $SYSCALL32
Pawlinho
  • 69
  • 6
  • 2
    You're missing commas, e.g. `cmp $counter %eax` should be `cmp $counter, %eax`. – sj95126 Mar 06 '22 at 18:29
  • 1
    What is `addl $13 message($counter)` supposed to do? – fuz Mar 06 '22 at 18:59
  • 1
    You can have a counter stored in memory. You can `dec`, `inc`, `add`, `sub` on a memory location. Everyone else uses a register as a counter because modifying memory is a lot slower than modifying a register value. – xiver77 Mar 06 '22 at 19:07
  • @fuz It is supposed to add 13 to value stored at the address pointed by message shifted by counter value – Pawlinho Mar 06 '22 at 19:21
  • It has nothing to do with ABI really. If you have a complicated loop with heavy register usage there's no reason why you can't use memory as a loop variable, which is perhaps accessed only once per iteration. – Weather Vane Mar 06 '22 at 19:21
  • 2
    @Pawlinho X86 does not have any double-indirect addressing modes. The index must be a register. Refer to the Intel Software Development Manuals for details. – fuz Mar 06 '22 at 19:27
  • 1
    Array indices *must* be registers. If you want to index an array with your loop counter, it therefore must be in a register. (Or get loaded into a tmp register at that part of the loop like debug-mode compiler output, but if you're running low on registers it's a good idea to make sure the a loop counter can be in one, even if that means spilling something else.) – Peter Cordes Mar 06 '22 at 20:41

0 Answers0