1

Aperrently, 32-bit system calls are not supported under WSL
Thank you Timothy Baldwin!
I made a simple program to return the highest element of an array, but $ echo $? echos 1, instead of 222
I am using as GNU assembler

/*
%edi will hold the current position in the list.
%ebx will hold the current highest value in the list.
%eax will hold the current element being examined
*/

# data_items - contains the item data. A 0 is used
# to terminate the data

.section .data

data_items: #These are the data items
.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0


.section .text

.globl _start

_start:
  movl $0, %edi                  # zero into index register 
  movl data_items(,%edi,4), %eax #load the first bit
  movl %eax, %ebx                # since this is the first item, %eax is the biggest

start_loop:                      # start loop 
  cmpl $0, %eax                      # check if element==0
  je loop_exit                   
  incl %edi                      # load next element
  movl data_items(,%edi,4), %eax
  cmpl %eax, %ebx                # compare value
  jle start_loop                 # jump to beginning if element is not the largest


  movl %eax, %ebx                # move the value as the largest

  jmp start_loop                 # go to beginning

  loop_exit:
    movl $1, %eax                # exit

  int $0x80                      #call kernel
  • 1
    AT&T syntax is the other way round. Therefore, change `cmpl %eax, %ebx ` to `cmpl %ebx, %eax`. – rkhb May 29 '18 at 20:09
  • Don't edit your question to say "Solved", that's not how we do things on Stack Overflow. Click the checkbox under an answer to mark it accepted. (If there are no existing answers which contain the actual solution, write an answer. Do not edit the answer into the question. If you do that, it's not a question anymore.) – Peter Cordes Jul 18 '18 at 06:15
  • I'm surprised this ran at all under WSL, unless you built a 64-bit executable out of this source that looks like it's written for 32-bit mode (32-bit addressing modes as well as 32-bit system calls.) Was there any error message from the shell, like segmentation fault? – Peter Cordes Jul 18 '18 at 06:18
  • no there wasn't – Screwballer Jul 18 '18 at 15:14
  • @Peter Cordes none of the answers solved it, it was a comment – Screwballer Jul 20 '18 at 14:18
  • Someone may argue this isn't a duplicate of the one I have identified. Not an exact duplicate by any means, but the duplicate and the answer does capture the essence of the issue - WSL won't run 32-bit executables. If you assemble 32-bit code as 64-bit code using `int 0x80` 32-bit system call interface that isn't supported either. You'll need to write your code as 64-bit and use the 64-bit `syscall` interface for it to work under WSL. – Michael Petch Jul 20 '18 at 21:14
  • can you please provide a link – Screwballer Jul 21 '18 at 00:29
  • Since I marked it as a duplicate the duplicate link is now above your question. – Michael Petch Jul 21 '18 at 10:08
  • @Screwballer: like I said in my comment, if none of the *answers* have the answer, then write one yourself. (Even if you repeat what was said in comments). Remember, this is for the benefit of future readers who have the same problem: we want the correct answer to be there at the top, not buried in with other comments. You don't need to do anything in this case because there's already another question where that was the answer, so \@MichaelPetch marked the question as a duplicate. – Peter Cordes Jul 21 '18 at 21:41
  • i dont have enough rep – Screwballer Jul 22 '18 at 01:41

1 Answers1

0

Your code is mostly correct, but the error is caused by a wrong Jcc instruction. So change the line

jle start_loop   # jump to beginning if element is not the largest

to the right Jcc instruction

jge start_loop   # jump to beginning if element is not the largest

and your code works as expected.

zx485
  • 28,498
  • 28
  • 50
  • 59