View Full Version : Assembly v.s. C/C++ ??
Hey, I've recently started to try and learn assembly language ive so far gone about achapter into it.. I kind of find it pretty inetersting so far.. just been reading about how it works with individual bits, bytes, words, and such.. I've so far, for the sake of understanding how it works, have studied how to convert decimal to hex, hex to decimal, decimal to binary, binary to decimal, hex to binary on paper, or in my head. Last night i wrote a little javascript that finds the highest power using any defined number base of any inputed number.. to like convert decimal to binary. But anyway... Before diving right into this 1400+ page book... I want to know If after learning Assembly I will be able to do anything that C or C++ can do ? how about vice versa, can c/c++ do anything that assembly can do? I do realize that in assembly you must write much code.. but im not concerned too much with that.. any input is appreciated. thanks for your time.
oracleguy
02-15-2006, 11:55 PM
Well you can definetly do in assembly anything you can do in C/C++ but I'm not sure if you'd want to. If you look at some of the functions in the standard C library, they are writtten in assembly. You can mix in assembly with your C/C++ code if you want.
You wouldn't want to write a lot of programs in assembly when you could write it in C/C++ just because its too low level to make it practical. But ASM has its purposes just like any other language.
Ah i see. If i were to learn assembly would it be possible to write your own language with it? I mean something like making libraries of functions and stuff, maybe like a simple version of Shell scripting. So one would not have to type out all of the assembly instructions all the time. Could this be done with C/C++ ?
slushy77
02-16-2006, 01:09 AM
Hi Ubik,
high-level languages where invented so that we dont have to remember assembly code and code can be ported to different machines easily.
assembly is used if you need to access the hardware or make sure that basic arithmetic/comparisons are being done effeciently.
In other words assembly is used to precisly control what a processor does. this is best illustrated with the following generic high-level code.
x = b + (x*c);
what the machine will be doing is
fetch x from memory, place in register1
fetch c from memory, place in register2
mutiply register1, register2
fetch b from memory, place in a register1
add the result register to register1
store the result register in the memory allocated to x
thats at least 7 operations :eek: ,or 7 lines of code.
whilst it is possible to write functions in asm, it should only be done when necessary - otherwise you'll be writing code for ages before you start seeing results, and you'll have lots more code to debug
oracleguy
02-16-2006, 01:57 AM
Ah i see. If i were to learn assembly would it be possible to write your own language with it?
Basically yes, but you can also do that in like C++ if you wanted.
4,000th Post
drhowarddrfine
02-16-2006, 02:32 AM
ubik,
Welcome to the land of assembly where everything is a DWORD. If you would like to join those of us who are enlightened and shun higher level languages, visit the Masm forum (www.masmforum.com) and WinAsm (http://win.asmcommunity.net/board/index.php)
To slushy77:
Yeah I get what you're saying but I dont know there's a curious cat inside of me that wants to learn how computers function. I started reading into some C++ book today and in the first chapter it was talking about how before Assembly, programmers would use the front panel to toggle in binary machine instructions.. thats hardcore, lol. I didnt think Assembly was that old, but i dont know those 7 lines didn't look too bad to me. I kinda like the idea that I would be in control of everysingle bit running through my prog. lol. Communicating directly with hardware.. yeah that sounds like fun.
To oracleguy:
I see, yeah I'll probably end up trying to learn both anyway.. just that ASM looked a little more interesting to me, being old school & all. congrats on your 4000th post.
To drhowarddrfine:
lol @ "shun higher level languages". Thanks for the link. I went to the Masm forum to check it out and stayed for a while reading alot of code.. looked like some good stuff. Hopefully I'll be in there eventually, trying to get someone to optimize my code, heh.
Thanks for your input all, i appreciate it.
drhowarddrfine
02-16-2006, 02:14 PM
From reading your posts, I don't think assembly is for you. The guys on the masm board won't mind helping you figure out how to optimize your code but they won't be interested in writing it for you. If you aren't interested in writing out all the asm code then asm isn't for you.
Like slushy said, before HLLs there was assembly and before assembly there was machine code and microcode, ie, coding in binary. Yes, I've done that. I've never had any problems writing in asm and spend more time debugging hll code. Probably because I started out designing the hardware at the chip (TTL) level. So when I write mov eax,[data], I picture in my mind the data actually moving from memory through the cpu into the register.
HLL programming caues me trouble due to its abstraction at any level because I can't always picture it the same way. In fact, the group I worked with was forced to learn C because the boss wanted us to and it caused us much frustration because it didn't always do what we wanted it to and it was more difficult to find out what was wrong.
slushy77
02-16-2006, 07:11 PM
If you want to learn to program in ASM, then go for it.
The early lunar missions used op-amps and variable-resistors for their computations - lol now thats true hardcore.
but i dont know those 7 lines didn't look too bad to me lol :) the point i was making was that each line of c/c++ would be mulitplied by an average of 7 in ASM. If you have a c program that is implemented in 1000 lines of code, the ASM equivalent could be anywhere from 1000 to 14,000 lines. Of course, assembly language looks nothing like what i wrote, thats just a plain english description of whats going on.
SpirtOfGrandeur
02-16-2006, 10:01 PM
Yeah I get what you're saying but I dont know there's a curious cat inside of me that wants to learn how computers function.
Take a comp arch course... that is when you learn how computers function! Not by programming on them, but by creating the chip structure!
drhowarddrfine
02-16-2006, 10:18 PM
If you have a c program that is implemented in 1000 lines of code, the ASM equivalent could be anywhere from 1000 to 14,000 lines.Probably not. A good asm coder can implement shortcuts a compiler may not try. Many asm programs are much, much shorter than one written in another language. But then we'll get into this whole argument of compilers being able to outthink a human while optimizing and the argument never ends because there are too many variables. Suffice to say, a C compiler can do it quicker but an asm coder can do it better. (After all, an asm coder may have made the C compiler)
to drhowarddrfine:
I was not implying that I would not want to write out all of the assembly instruction and learn all of the ASCII charcters by code. I was just being optimistic, in saying that perhaps one day i would know it well enough to write my own simple language.
to SpirtOfGrandeur:
Take a comp arch course... that is when you learn how computers function! Not by programming on them, but by creating the chip structure!
hmm sounds good. So ive been looking into comp architecture as well. In the first chapter i guess they start off with boolean logic right. explaining AND OR and NOT, then they introduce NAND, NOR, XOR, and XNOR...
Here's what I think all this means, please correct me because i think im wrong, AND multiplies the two operands. OR adds the two operands up, only that 1 + 1 = 1. NOT inverts both operands. NAND multpilies the two operands then inverts the result. NOR adds up the two operands then inverts the result, except that 1+1=1 inverted = 0. But it really doesnt explain XOR or XNOR at all really.. by lookin at this picture:
http://xs68.xs.to/pics/06075/booleanLogic.gif
I dont really understand how XOR or XNOR work it looks like they're the same as the regular OR and NOR except when it comes to the 1 + 1.. any help is appreciated. I just dont like reading books and skipping texts when there's something I dont understand.
EDIT:
nevermind i googled "define:XOR"
A Boolean operator that returns a value of TRUE only if just one of its operands is TRUE. In contrast, an inclusive OR operator returns a value of TRUE if either or both of its operands are TRUE.
wonder why that book did not define that. hm. It's "McGraw Hill - Complete Digital Design A Comprehensive Guide to Digital Electronics and Computer System Architecture"
oracleguy
02-17-2006, 03:35 AM
Probably not. A good asm coder can implement shortcuts a compiler may not try. Many asm programs are much, much shorter than one written in another language.
I seriously hope you mean by the final code generated and not the code that the programmer had to write. If we are talking the work the programmer actually has to do, then something higher level than assembly is definetly going to be shorter.
Here's what I think all this means, please correct me because i think im wrong, AND multiplies the two operands. OR adds the two operands up, only that 1 + 1 = 1. NOT inverts both operands. NAND multpilies the two operands then inverts the result. NOR adds up the two operands then inverts the result, except that 1+1=1 inverted = 0. But it really doesnt explain XOR or XNOR at all really.. by lookin at this picture:
Not quite. An AND just ands the inputs together, so if you have to inputs, A nd B, if A and B are both one, it outputs a one, otherwise it is a zero. A NAND works the same way excepts inverts the output.
An OR gate is just like the name implies, if A or B are 1, then it outputs a one.
An XOR is an exclusive OR gate, meaning if A or B are 1 but not A and B, then it will output a 1.
drhowarddrfine
02-17-2006, 03:38 AM
Although AND and OR are used in multiplication circuits, you shouldn't be thinking along those lines while learning Boolean. It is the logic of the inputs producing the outputs, those rules, that are important. If you have two bytes of eight bits each: 00000001 with 00000010 which you AND together, the result is 00000000 but if you OR them you would get 00000011.
ANDing, in assembly, is sometimes used for testing whether a bit is on or off. So in the first byte ending with 1, if I AND it with 00000001, the result will be a positive one, possibly indicating TRUE, and we can jnz (jump not zero) or other op to go to a subroutine to handle that.
Although AND and OR are used in multiplication circuits, you shouldn't be thinking along those lines while learning Boolean.
Yes I see what you mean as both, you and oracleguy stated above. It makes more sense the way you guys see it, and i thank you for sharing that perspective. Its just that in this book im reading right now, it portrays these Boolean operators in a mathematical fashion:
A common means of representing the output of a generic logical function is with the variable Y. Therefore,the AND function of two variables, A and B, can be written as Y =A & B or Y =A*B. As with normal mathematical notation, products can also be written by placing terms right next to each other, such as Y = AB
drhowarddrfine
02-17-2006, 04:05 AM
I seriously hope you mean by the final code generated and not the code that the programmer had to write. If we are talking the work the programmer actually has to do, then something higher level than assembly is definetly going to be shorter.Whoops. Yep. I was thinking of the final program generated. I was dreaming of the days of "I can make that same thing in a 8K ROM". "Yeah, well, I can do it in less than 1K" "Oh, yeah, well ...."
Not quite. An AND just ands the inputs together, so if you have to inputs, A nd B, if A and B are both one, it outputs a one, otherwise it is a zero. A NAND works the same way excepts inverts the output.
An OR gate is just like the name implies, if A or B are 1, then it outputs a one.
An XOR is an exclusive OR gate, meaning if A or B are 1 but not A and B, then it will output a 1.
that's a very good explanation BTW, thanks again. very easy to understand.
oracleguy
02-17-2006, 04:50 AM
Its just that in this book im reading right now, it portrays these Boolean operators in a mathematical fashion:
A common means of representing the output of a generic logical function is with the variable Y. Therefore,the AND function of two variables, A and B, can be written as Y =A & B or Y =A*B. As with normal mathematical notation, products can also be written by placing terms right next to each other, such as Y = AB
Well that is true, you do see boolean equations written in that fashion; however it is definetly not multiplication, your book should of clarified that more.
Like if you see:
Y = AB + C
Translated would be:
Y = A and B or C
SpirtOfGrandeur
02-17-2006, 11:54 AM
I was talking more along the lines of creating the registers and how the registers respond to certain commands. And how to create the commands to call the registers and such. Creating busses, timing the cycles, and of course the fun parts as in creating logic chips and designing new functions you think your chip should have!
Then comes the fun of AND, OR, NO, XOR... Actually as a MCS, these where my funnest courses!
drhowarddrfine
02-17-2006, 02:21 PM
PALs (Programmable Array Logic) are collections of gates and flip-flops on one chip. To program them you would use Boolean and a program, such as Able. It would be all boolean and it would burn the chip. But screw up the equation and you would throw away a $5 chip. (It wasn't eraseable back then).
Ender
02-18-2006, 03:27 AM
C++ inline assembly != assembly.
You can do almost everything you can do with assembly in inline assembly, and almost everything is the same. There are a few limitations (i.e. no assembler operators besides PTR, no macro directives), but the main reason for my above statement is that C++ inline assembly is still contained in a C++ source file nonetheless. When using inline assembly in a higher-level language, you lose the portability inherent in assembly language. So if you want to do embedded systems programming or cross-platform BIOS programming, you should learn how to program assembly with an assembler.
If you're going to use C++ inline assembly, you're going to have to learn ASM anyways. Assembly is great for learning how a computer works, but if you're not ready for getting into computer architecture, and want to make large programs instead, then you should learn a high-level language.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.