Bona Fide OS Development
http://forums.osdever.net/

C to Asm
http://forums.osdever.net/viewtopic.php?f=5&t=127
Page 1 of 1

Author:  64RB463_F173 [ Fri Jan 15, 2010 2:49 pm ]
Post subject:  C to Asm

I keep getting told that C is so much simpler to use but the logic of ASM makes so much more sense to me and for some reason C just seem sto confuse the hell out of me :( so since all the tutorials i read are pretty much in C/C++ im trying to convert it to ASM but im not positive if this is correct or not:
C code from http://www.jamesmolloy.co.uk/tutorial_html/3.-The%20Screen.html
Code:
// Updates the hardware cursor.
static void move_cursor()
{
   // The screen is 80 characters wide...
   u16int cursorLocation = cursor_y * 80 + cursor_x;
   outb(0x3D4, 14);                  // Tell the VGA board we are setting the high cursor byte.
   outb(0x3D5, cursorLocation >> 8); // Send the high cursor byte.
   outb(0x3D4, 15);                  // Tell the VGA board we are setting the low cursor byte.
   outb(0x3D5, cursorLocation);      // Send the low cursor byte.
}


and my ASM version:
Code:
mov ax, X_VALUE
mov bx, Y_VALUE

move_cursor:         ;
   mov dx,bx         ; copy bx (y) to dx
   shl bx,6         ; bx * 64
   shl dx,4         ; dx * 16
   add dx,bx         ; add both together (y * 80)
   add ax,dx         ; add dx to ax (y*80 + x)
               ;
   out 0x3d4, 14      ; tell vga to set high byte
   out 0x3d5, ah      ; send high byte
   out 0x3d4, 15      ; tell vga to set low byte
   out 0x3d5, al      ; send low byte

Author:  DudeOfX [ Fri Jan 15, 2010 9:01 pm ]
Post subject:  Re: C to Asm

well thats an interesting argument... and btw you also have an interesting user ID... your assembly looks right thou... I like assembly because there is no ambiguity...

Author:  Arlen [ Sat Jan 16, 2010 8:10 pm ]
Post subject:  Re: C to Asm

Honestly, there's no ambiguity either, in C (or C++), as long as you understand the quirks and how the language works and compiles down into assembly. Unfortunately, there are many of those, but a strong knowledge of same means you can be much more efficient coding. (that's why higher-level languages exist!)

Author:  XanClic [ Wed Jan 20, 2010 10:44 am ]
Post subject:  Re: C to Asm

I'm aware that this code won't even assemble:
Code:
   out 0x3d4, 14      ; tell vga to set high byte
   out 0x3d5, ah      ; send high byte
   out 0x3d4, 15      ; tell vga to set low byte
   out 0x3d5, al      ; send low byte

If the port number doesn't fit in a byte, you have to put it into DX and use that register. Furthermore you may only output AL and no other value (neither a constant value nor another register nor a memory area). So try it this way:
Code:
   push ax
   mov al,14
   mov dx,0x3D4
   out dx,al
   inc dx
   mov al,ah
   out dx,al
   dec dx
   mov al,15
   out dx,al
   inc dx
   pop ax
   out dx,al


Hope that helps.

Author:  64RB463_F173 [ Sun Jan 24, 2010 2:53 am ]
Post subject:  Re: C to Asm

Ok yeah i over looked that (or am ignorant) thanx, makes sense tho.

Code:
   push ax
   mov al,14
   mov dx,0x3D4
   out dx,al
   inc dx
   mov al,ah
   out dx,al
   dec dx
   mov al,15
   out dx,al
   inc dx
   pop ax
   out dx,al


Ive just recently started learning about using ports in asm so..... Thanx for all the replies.

Page 1 of 1 All times are UTC - 6 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/