Memory mapped I/O and inline assembly

Programming, for all ages and all languages.
Post Reply
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Memory mapped I/O and inline assembly

Post by mark3094 »

My question is about using inline assembly to create a function that replaced the IN and OUT instructions.

I'm working with C in Visual Studio 2010, and I can get things working if I use a separate NASM file. Eg, to setup the PIC, I use this:

Code: Select all

	MOV		AL, 0x11
	OUT		0x20, AL
	OUT		0xA0, AL

	MOV		AL, 0x20
	OUT		0x21, AL
	MOV		AL, 0x28
	OUT		0xA1, AL

	MOV		AL, 0x4
	OUT		0x21, AL
	MOV		AL, 0x2
	OUT		0xA1, AL

	MOV		AL, 0x1
	OUT		0x21, AL
	OUT		0xA1, AL
...which works just fine. However I'm trying to create an outb function. I modified this one from the web:

Code: Select all

void outb (unsigned short portid, unsigned char value) {
	__asm {
		mov		al, byte ptr [value]
		mov		dx, word ptr [portid]
		out		dx, al
	}
}
I try to configure the PIC using this code, but it does not work:

Code: Select all

	outb(0x20, 0x11);
	outb(0xa0, 0x11);

	outb(0x21, 0x20);
	outb(0xa1, 0x28);

	outb(0x21, 0x4);
	outb(0xa1, 0x2);

	outb(0x21, 0x1);
	outb(0xa1, 0x1);
I can't see where I'm going wrong. Can anyone help me?
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Memory mapped I/O and inline assembly

Post by xenos »

Have you disassembled the output file and checked the code that is generated?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Memory mapped I/O and inline assembly

Post by mark3094 »

This is the (partial) output produced by the outb function:

Code: Select all

_outb	PROC
	push	ebp
	mov	ebp, esp

	mov	al, BYTE PTR _value$[ebp]
	mov	dx, WORD PTR _portid$[ebp]
	out	dx, al
	pop	ebp
	ret	0
_outb	ENDP
I can't see anything wrong (yet)...
User avatar
xenos
Member
Member
Posts: 1118
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Memory mapped I/O and inline assembly

Post by xenos »

Looks fine to me as well...

The next thing I'd recommend is to run the code in bochs (maybe with debugger enabled in single-step mode) and to set the debug log level for the PIC to "report" (i.e., go to the config menu, log options for individual devices, PIC, debug, report).
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Memory mapped I/O and inline assembly

Post by mark3094 »

I had a rethink of my strategy, and have replaced the inline assembly with a separate asm file.
I pass values to the asm object via the stack.

This is working well now.
Post Reply