-1

I'm trying to copy the 2nd argument to an array declared as: .bss X: resb 20

I don't know how to approach this. Here's my attempt:

The argument ends in a terminating (0x0) character.

%include "asm_io.inc"
global asm_main

section .data

section .bss
  temp: resd 1
  i: resd 1
  X: resb 20

  length: resd 1 ; length of input

section .text

asm_main:
  enter 0, 0
  pusha

  mov ebx, dword [ebp+12]
  mov esi, dword [ebx+4]
loop: lodsb
  ; end of string?
  or al, al
  jz endloop

  ; copy arg[1] to x ================
  mov temp, X         ;error; temp = address of X 
  add temp, dword [i] ;error; temp += i
  mov byte [temp], al ;     ; temp[i] = al
  add dword [i], 1          ; i++
  ; =================================

  jmp loop
endloop:
  popa
  leave
  ret

I don't think I'm copying the array address correctly, it won't compile ^ The error is: "invalid combination of opcode and operands" for the two lines marked

Wulf
  • 153
  • 1
  • 8
  • `ebp` is not initialized at all. You need to set it up yourself if you intend to use it. – Jester Dec 17 '15 at 00:41
  • I didn't put all the code ^, I can if you need it. I've checked that it works, and have even printed the argument. Added SNIP lines. – Wulf Dec 17 '15 at 00:42
  • See [MCVE]. Also, _won't compile_ is a bad problem description, you should at least provide error message and mark the relevant line. That said, `mov dword [temp], al` is definitely wrong since `al` is not a dword. Not sure what you wanted to do with the `temp` anyway. – Jester Dec 17 '15 at 00:44
  • 1
    Sorry, I just thought it was a simple thing that I was overlooking. Edited ^ – Wulf Dec 17 '15 at 00:50
  • 4
    x86 cannot take memory for both two operands. You have to use registers for at least one operand of `mov` and `add`. – MikeCAT Dec 17 '15 at 00:53
  • MikeCAT, which register can I use? I don't really know how LODSB works, and I'm scared that I might mess with the array if I use one of the exx registers. Please advise. – Wulf Dec 17 '15 at 00:57
  • Michael Petch, nope, no one named Sally :) – Wulf Dec 17 '15 at 00:57
  • Probably used a fake name on Stackoverflow ;). The question being asked seems similar to some of hers http://stackoverflow.com/questions/34302055/loop-to-check-lowercase-letter-in-assembly – Michael Petch Dec 17 '15 at 01:00
  • Yup, that's the same assignment alright. – Wulf Dec 17 '15 at 01:04
  • If it's the same assignment, you don't really need to copy the argument, do you? XY problem ... – Jester Dec 17 '15 at 01:10
  • Have a look at http://stackoverflow.com/questions/34058101/referencing-the-contents-of-a-memory-location-x86-addressing-modes. It explains the difference between immediate operands (`value`) and memory operands (`[mem_location]`). – Peter Cordes Dec 17 '15 at 01:19
  • Same project. Seems like lately a lot of people are asking about "maxLyn". – Steven Dec 17 '15 at 03:22

1 Answers1

3

Since you can't address using a variable in memory, your temp is useless. You have to pick a register for it, say, edi. Then that block might look like:

  mov edi, X
  add edi, [i]
  mov [edi], al

This is of course overcomplicated, as is frequently the case with beginner code. A simpler version could look like:

  mov esi, [ebp+12]
  mov esi, [esi+4]
  mov edi, X
loop:
  lodsb
  stosb
  ; end of string?
  or al, al
  jnz loop

I don't really know how LODSB works

You should then read the instruction set reference page about it. It uses esi and al but you already know that, why else would you load esi with the address and expect the result in al otherwise?

Jester
  • 56,577
  • 4
  • 81
  • 125