0
#include <stdio.h>
#include <serial.h>

extern void echo(int conport, unsigned char esc_char);
int main()
{
  int console = sys_get_console_dev(); /* find out current sys console */
  int conport;
  char escape;

  switch (console) {
  case COM1: conport = COM1_BASE;
    break;
  case COM2: conport = COM2_BASE;
    break;
  default: printf("Expected serial port for console, exiting\n");
    return 0;
  }

  printf("Type escape character and enter\n");
  if (scanf("%c\n", &escape) != 1)
    escape = 0x01;
  echo(conport, escape);
  return 0;
}

I'm making an x86 program to basically just scan and output chars from an IO device, but I can't figure out how to use the in and out instructions correctly. I keep getting operand size mismatch and I've tried all the different sizes. The C code above is whats calling the x86 code below

.globl echo
.text
# first argument is int port, second argument is escape char
prologue:   
    push %ebp       #push old ebp to top of stack
    movl %esp, %ebp     #point ebp to same thing as esp, both pointing to old ebp

    #saving live registers
    push %esi
    push %edi
    push %ebx   

    #take values from stack
    movl 8(%ebp), %ebx  #move connport int to ebx
    movb 12(%ebp), %cl  #move esc char to be counted to ecx
    
echo:
    inb (%ebx), %dl     #put input of conport ebx into dl
    cmp %dl, %cl        #cmp dl and esc char
    je end          #jmp to end if eq
    outb %dl, (%ebx)        #output %dl to conport
    jmp echo
    
end:
    pop %ebx
    pop %edi
    pop %esi
    movl %ebp, %esp
    pop %ebp
    ret

phuclv
  • 37,963
  • 15
  • 156
  • 475
pneuc4353
  • 21
  • 2
  • 1
    Read the manual: https://www.felixcloutier.com/x86/out - the port number and data can only be in fixed registers. (DX and AL, so you don't need to use any call-preserved registers, thus don't need to push/pop anything.) – Peter Cordes May 20 '22 at 01:19
  • And yes, GAS has unhelpful error messages that sometimes don't match the real problem. I prefer NASM, but if you know how instructions work, or don't mind reading the manual carefully, GAS is usable. – Peter Cordes May 20 '22 at 01:58
  • Almost duplicate: [Why does AT&T syntax use parens around DX in IN / OUT instructions like inb (%dx),%al](https://stackoverflow.com/q/71231398) show the correct forms of `inb`. – Peter Cordes May 20 '22 at 05:01
  • 2
    Also, note that 'in' and 'out' are privileged instructions. They aren't going to work at all for some random user process on anything like a modern operating system. – SoronelHaetir May 20 '22 at 06:50

0 Answers0