• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

I'm trying to write a program that converts an integer into a string. But I'm having trouble because neither my textbook nor the professor really covered STOSB, and we're required to use it.

Code:
writeVal PROC
pushad

mov eax, [esp+40] ;number to convert to string
mov edi, [esp+36] ;tempString address
mov ebx, 10 ;will need to divide by 10

std

numToString:
	mov edx, 0
	div ebx ;divide by 10
	add edx, 48 ;add 48 to convert from int to ascii
	push eax
	mov eax, edx
	stosb
	pop eax
	cmp eax, 0
	jne numToString


mov edx, [esp+36]
call WriteString

popad
ret 8
writeVal ENDP

Numbers are entered 10 at a time into an array. They are then passed individually (looping through the array) to this procedure.

Any help would be appreciated. I'm getting really odd results.

Like entering: 15, 23, 222, 445, 232, 12, 112, 3, 21, 2

gives: 15
23
,222
,445
,232
212
,112
13
121
22

So I'm getting random commas, and just plain wrong numbers at certain points.

http://www.intel.com/content/dam/ww...r-instruction-set-reference-manual-325383.pdf

is a better reference than both your textbook and your teacher.

That said, you haven't posted the code that prints the comma or the newline, and "WriteString" seems kind of dubious. What does it do?

I also find it a little strange that you are not setting up a proper stack frame.
 
http://www.intel.com/content/dam/ww...r-instruction-set-reference-manual-325383.pdf

is a better reference than both your textbook and your teacher.

That said, you haven't posted the code that prints the comma or the newline, and "WriteString" seems kind of dubious. What does it do?

I also find it a little strange that you are not setting up a proper stack frame.

Wait how is my stack frame improper?

As for the comma, that's the thing. There IS nothing that should print a comma. Nowhere in my code. But a comma is getting printed.
 
Wait how is my stack frame improper?

As for the comma, that's the thing. There IS nothing that should print a comma. Nowhere in my code. But a comma is getting printed.

The normal way to set up a stack frame is like this:

Code:
push ebp
mov ebp, esp
sub esp, [number of bytes of local variables]

...

mov esp, ebp
pop ebp
ret [number of bytes of local variables]

The important thing is that the third line, which "reserves" space on the stack for local variables. This way if you call another function, that function does not see the any of the variables that the previous function wrote onto the stack. In your example, when you call WriteString, it has the same stack variables as writeVal, which is pretty weird. IImagine if you wrote a function in C which had some local variables and called another function, and the second function could use the first function's local variables.

Another problem: You use both pushad, popad, and ret. pushad will save the stack pointer, but then popad will restore it and ret will remove ANOTHER 8 bytes from the stack pointer. So your stack is getting all jacked up.

This is why people use the aforementioned pattern for "setting up a stack frame". With that you don't use pushad or popad, you only push / pop the registers you modify (and return value is usually passed through eax)


Also another problem: How does WriteString decide how many characters to write? It doesn't look like you're ever null terminating the string, so there's no way for it to know when to stop writing.
 
Alright I see what you are saying.

I actually did get my code to work by taking another approach. WriteString is a procedure from the Irvine library.

As for pushad/popad, I thought I still needed the ret (+X) statements.

For example

push eax
push ebx
call PROCEDURE

PROCEDURE PROC
pushad
popad
ret 8


Unless I've been mistaken this whole time.

And as for why I bothered using pushad/popad... our professor kind of said it was good to do so.
 
Alright I see what you are saying.

I actually did get my code to work by taking another approach. WriteString is a procedure from the Irvine library.

As for pushad/popad, I thought I still needed the ret (+X) statements.

For example

push eax
push ebx
call PROCEDURE

PROCEDURE PROC
pushad
popad
ret 8


Unless I've been mistaken this whole time.

And as for why I bothered using pushad/popad... our professor kind of said it was good to do so.

Easy way to check would be, before calling writeVal, print the value of ESP (or look at it in a debugger). After you return from writeVal, print the value of ESP again. They should be the same. Here's the documentation for PUSHAD, POPAD, and RET

PUSHAD said:
Pushes the contents of the general-purpose registers onto the stack. The registers are stored on the stack in the following order: EAX, ECX, EDX, EBX, ESP (original value), EBP, ESI, and EDI (if the current operand-size attribute is 32) and AX, CX, DX, BX, SP (original value), BP, SI, and DI (if the operand-size attribute is 16). These instructions
perform the reverse operation of the POPA/POPAD instructions. The value pushed for the ESP or SP register is its value before prior to pushing the first register (see the “Operation” section below)

POPAD said:
Pops doublewords (POPAD) or words (POPA) from the stack into the general-purpose registers. The registers are loaded in the following order: EDI, ESI, EBP, EBX, EDX, ECX, and EAX (if the operand-size attribute is 32) and DI, SI, BP, BX, DX, CX, and AX (if the operand-size attribute is 16). (These instructions reverse the operation of the
PUSHA/PUSHAD instructions.) The value on the stack for the ESP or SP register is ignored. Instead, the ESP or SP register is incremented after each register is loaded.

RET said:
The optional source operand specifies the number of stack bytes to be released after the return address is popped; the default is none. This operand can be used to release parameters from the stack that were passed to the called procedure and are no longer needed. It must be used when the CALL instruction used to switch to a new procedure
uses a call gate with a non-zero word count to access the new procedure. Here, the source operand for the RET instruction must specify the same number of bytes as is specified in the word count field of the call gate.

So, let's say you start out and your stack pointer is 0x1000, just to be easy. Then you call PUSHAD. According to the documentation, the following things happen:

Code:
PUSH EAX   ; ESP = 0xFFC
PUSH ECX   ; ESP = 0xFF8
PUSH EDX   ; ESP = 0xFF4
PUSH EBX   ; ESP = 0xFF0
PUSH ESP (original value)   ; ESP = 0xFEC
PUSH EBP   ; ESP = 0xFE8
PUSH ESI    ; ESP = 0xFE4
PUSH EDI    ; ESP = 0xFE0

Now you start running your function. You never actually modify the value of ESP anywhere (except a push followed by a pop), so at the end of the function you run POPAD. According to the documentation, it does this:

Code:
; ESP = 0xFE0
POP EDI   ; ESP = 0xFE4
POP ESI   ; ESP = 0xFE8
POP EBP  ; ESP = 0xFEC
; ignores the value of ESP, ESP = 0xFF0
POP EBX  ; ESP = 0xFF4
POP EDX  ; ESP = 0xFF8
POP ECX  ; ESP = 0xFFC
POP EAX  ; ESP = 0x1000

Finally you execute RET 8. This increments ESP by 8, and now you have ESP = 0x1008. So, when the previous function returns, and it tries to access ITS local variables by doing [esp+whatever], it will write into the wrong local variable.
 
Ah well that explains a bit. Thanks for the help man.

Anyway the main problem was not appending a 0 to the end of my strings. As you suspected.
 

Kieli

Member
Yeah, C# and Java are really similar. Sometimes I can look at code and not even be able to tell which one it is. Definitely do as much cursory brushing up on those web concepts that you're not familiar with. Hopefully they will mostly test you on fundamentals that carry over to any programming job rather than just specifics of a language you might not have tons of experience in.

I was told C# and Java were not very similar during an interview. The company primarily worked in C#.
 

Slo

Member
I was told C# and Java were not very similar during an interview. The company primarily worked in C#.

If you're talking Operating Systems with an old school mainframe command line programmer, Windows 10 and RedHat might as well be the same thing. If you're having the same discussion with a Linux cultist, they are worlds apart.
 

kadotsu

Banned
I want to get into functional programming but I have no idea where to start. My Comp. Eng. program did not have a module for it. Could someone point me to a good resource with a lot of examples. Language is a secondary concern. Haskell or F# would be a plus, though.
 

Makai

Member
I want to get into functional programming but I have no idea where to start. My Comp. Eng. program did not have a module for it. Could someone point me to a good resource with a lot of examples. Language is a secondary concern. Haskell or F# would be a plus, though.
http://learnyouahaskell.com

Switch to F# after youve learned enough to make a simple console app like tic tac toe. Hello world isnt until like chapter 9
 
Hey guys, thanks for all the previous advice.

At the moment I'm in the process of learning about regular expressions (Regex).

Why? Well it's something I know almost nothing about and I decided now was the time to get to grips with it before the books I ordered arrive.

I like to take things in small chunks/concepts at a time that I'm not familiar with or I find that I'm weak at and try to improve my knowledge.

So far the theory doesn't seem too bad or even nearly as bad as I originally assumed.

What I'm really looking for are any good exercises or series of exercises that utilize or would be best solved by utilizing regex. Learning theory is all well and good but without practical examples to solidify the knowledge I feel it will be a waste.

Anyone have any ideas?
 

Holden

Member
Hey guys, thanks for all the previous advice.

At the moment I'm in the process of learning about regular expressions (Regex).

Why? Well it's something I know almost nothing about and I decided now was the time to get to grips with it before the books I ordered arrive.

I like to take things in small chunks/concepts at a time that I'm not familiar with or I find that I'm weak at and try to improve my knowledge.

So far the theory doesn't seem too bad or even nearly as bad as I originally assumed.

What I'm really looking for are any good exercises or series of exercises that utilize or would be best solved by utilizing regex. Learning theory is all well and good but without practical examples to solidify the knowledge I feel it will be a waste.

Anyone have any ideas?

https://regexcrossword.com/

funfunfun



learning compilers can be fun tooooo :)
 

poweld

Member
I'd say half of this at least is a joke, a way to vent frustration. I think that more than often, if you're against a language, you've practiced it, suffered because of it, and try to recover.
It's a real concern of mine. Again, not for the people that are willing to form their own opinions, but for people that will just accept other's opinions as fact. Maybe I shouldn't worry so much, though.

If Scala can be a substitute to Java, what kind of use I would have for Java? ;)
I do wish it could be. I prefer writing Scala over Java for personal projects, but for large projects I'd lean towards Java. The compile times are quite long (as of 2.10), plus I've spent measurable time staring at problematic Scala code just baffled, where I've found Java to at least be pretty easy to understand.

Here's some reasons why:
  1. The ability to name methods just about anything, in conjunction with infix notation for arity-1 methods. I can create a method with a rad name
    Code:
    def /<@>/(x: Any): Unit = println("there is no help for you")
    and call it like this
    Code:
    scala> foo /<@>/ "god help me"
    there is no help for you
    I wouldn't mention it if this didn't really happen all the time, even in the standard library. For example, I hope you remember which method is which, based on the order and direction of the slash: http://www.scala-lang.org/api/2.11....tion.immutable.List@/:[B](z:B)(op:(B,A)=>B):B vs http://www.scala-lang.org/api/2.11....tion.immutable.List@:\[B](z:B)(op:(A,B)=>B):B Not to mention that in this case, these are aliases for the reasonably well-named methods foldLeft and foldRight
  2. Implicit classes are the new goto, worse even. Even goto has its place. Implicit classes immediately make some or all of your code inscrutable and impossible to debug. Wrapping as an alternative, while bloating your wrapper, is at least easy to make sense of.
  3. Just because you don't need to declare the type of your objects or methods doesn't mean you shouldn't. The entire Kafka codebase, for example, doesn't explicitly declare types, making it just a little harder to understand what's going on. Granted this may be for ease of refactoring, but c'mon, we're using pretty amazing IDEs that can do intelligent search and replace really well.

On the other hand, its pattern matching, for comprehension, immutability, and lispy collection manipulation can make it such a delight.

I guess I had a lot to get off my chest about Scala.
 

Koren

Member
It's a real concern of mine. Again, not for the people that are willing to form their own opinions, but for people that will just accept other's opinions as fact. Maybe I shouldn't worry so much, though.
I'm under the impression that language wars exist since the days of ALGOL... Even if some people make a decision for a bad reason, given that you have to make choices, I'm not sure it matters that much. Popular languages under fierce attacks stay popular. With very much every languages, you have supporters and people hating them.

I'd be curious to know why you care exactly (I think I started not caring for everyone is seeing after having heard so many times "x86 assembly is awful, RISC is the way to go". While I find plently of advantages in RISC, I still prefer x86, probably mostly out of habit, partly because not having useful instructions to keep the instruction count low or keep them with a fixed width (e.g. load immediate limitations) doesn't seem that appealing for the coder)

I do wish it could be. I prefer writing Scala over Java for personal projects, but for large projects I'd lean towards Java.
I can understand (I just use something else for large projects myself)

Can't say I disagree with your examples, although /: and :\ clicked for me (probably because I see /: as &#8625;. so it has visual meaning... But maybe it's because I struggled a bit with it_list and list_it in Caml in the 90s and Scala fell right into my representation of the trick).

I guess I had a lot to get off my chest about Scala.
I remember reading about the steps you go through when learning Scala, with a WTF in the middle. It's actually quite true.
 

poweld

Member
I think implicit arguments are the worst, but when has an implicit class conversion ever bitten you? Isn't it just a clever way to add behaviors to objects?

You're right, it's been a while. Implicit arguments are the real devil.

But implicit classes are also pretty ugly. IIRC, the implicit class that gets used is determined by lexical scoping. So you could have two different implicit classes extending the same base class adding the same method. Not very fun to find out which one is being used.

I'd be curious to know why you care exactly
It may be just an idealistic view of mine. I try to maintain the idea that people are good, and that misinterpretation or miscommunication is usually the root cause of ill feelings towards another.

Most languages have something to offer, and aren't just "bad".

I just want people and languages to get along, goddamnit.
 

Koren

Member
Koren, what's your assessment of the numerous features and differences Scala makes over ML? What works, and what doesn't?
Not sure I can answer to that... What are you talking exactly about?

There's a couple of things in ML I miss in Scala, such as some type inference (especially for recursive functions), or the different take on curryfied expressions, partial expressions... I heard that most of those are because of Java limitations (I've never dived into the details, though).

Scala has the advantage of the ecosystem, though.

It seems that the C# -> F# will be more successful in bringing a ML-like language over a successful basis than Java -> Scala is. But as a JVM language, I still think Scala is nice (at still for small things).

It may be just an idealistic view of mine. I try to maintain the idea that people are good, and that misinterpretation or miscommunication is usually the root cause of ill feelings towards another.

Most languages have something to offer, and aren't just "bad".

I just want people and languages to get along, goddamnit.
I can understand, but I'm not sure it's that a serious question. People are vocals about their opinions on all topics, languages aren't different. But maybe that's just me.
 

poweld

Member
So honest question, what do you think Java offers that C# doesn't?

I never claimed that C# offers less or more than Java. I pointed out some feature parity when that came up.

But to answer your question as someone who has never written a line of C#, I think that Java offers a larger library to work with, due both to its ubiquity and age. Other than that, I couldn't say more, due to my lack of familiarity.

edit: Wowza, C# is older than I thought (~15y, not ~10y like I thought)! I suppose you can nix the age bit.
 

JesseZao

Member
I never claimed that C# offers less or more than Java. I pointed out some feature parity when that came up.

But to answer your question as someone who has never written a line of C#, I think that Java offers a larger library to work with, due both to its ubiquity and age. Other than that, I couldn't say more, due to my lack of familiarity.

edit: Wowza, C# is older than I thought (~15y, not ~10y like I thought)! I suppose you can nix the age bit.

You're never written in C# and you love Java. We get it.
 
I never claimed that C# offers less or more than Java. I pointed out some feature parity when that came up.

But to answer your question as someone who has never written a line of C#, I think that Java offers a larger library to work with, due both to its ubiquity and age. Other than that, I couldn't say more, due to my lack of familiarity.

edit: Wowza, C# is older than I thought (~15y, not ~10y like I thought)! I suppose you can nix the age bit.

You should try it. I mean, most of us shitting on Java I think have tried both languages and just come to our own conclusion that C# offers everything that Java does, but does it better. It's not that we're shitting on the language with no basis, it's just our experience tells us that head to head in terms of performance, enjoyability, expressiveness, elegance, and many other metrics, C# is just better.

Of course, this is all subjective, but you've said yourself you haven't tried it. So why not try it?
 

poweld

Member
You're never written in C# and you love Java. We get it.
I don't love Java. I responded to a question. Nice comment, though!

You should try it. I mean, most of us shitting on Java I think have tried both languages and just come to our own conclusion that C# offers everything that Java does, but does it better. It's not that we're shitting on the language with no basis, it's just our experience tells us that head to head in terms of performance, enjoyability, expressiveness, elegance, and many other metrics, C# is just better.

Of course, this is all subjective, but you've said yourself you haven't tried it. So why not try it?
I absolutely will try it. Previously it was restrictive due to the lock-in on Windows platforms. When I get a chance, it's next on my list.
 
You should try it. I mean, most of us shitting on Java I think have tried both languages and just come to our own conclusion that C# offers everything that Java does, but does it better. It's not that we're shitting on the language with no basis, it's just our experience tells us that head to head in terms of performance, enjoyability, expressiveness, elegance, and many other metrics, C# is just better.

Of course, this is all subjective, but you've said yourself you haven't tried it. So why not try it?

Eh, in terms of web application speed, pretty much every C# framework falls behind the average Java framework.
 

JesseZao

Member
You should try it. I mean, most of us shitting on Java I think have tried both languages and just come to our own conclusion that C# offers everything that Java does, but does it better. It's not that we're shitting on the language with no basis, it's just our experience tells us that head to head in terms of performance, enjoyability, expressiveness, elegance, and many other metrics, C# is just better.

Of course, this is all subjective, but you've said yourself you haven't tried it. So why not try it?

Bingo.
 
Not sure I can answer to that... What are you talking exactly about?
The whole zoo of features. Scala is maybe on par with Haskell when it comes to wild experimentation of language additions.

- Implicit parameters
- Implicit classes
- Call-by-name
- compound types
- "Case classes"
- Extractors
- the infamous Shapeless library
- traits
- Infix operators

Honestly, I feel very overwhelmed when I try to learn Scala!
 
Eh, in terms of web application speed, pretty much every C# framework falls behind the average Java framework.

Programmers are way to obsessed with speed. Perhaps Java frameworks are faster, but so what? Speed is rarely the issue. A site like Stack Overflow runs on c#/asp.net and i doubt most web developers work on sites that get more hits than that one.

If speed really was an issue for web sites everyone would run Erlang.
 
This is my third day writing Scala pretty much full time (coming from a modern JavaScript background) and it's been a good split of "I don't know what I am doing-dog".jpg and "I have some sort of idea what I am doing-dog".jpg
 

Ambitious

Member
As I still hadn't heard from the company I applied at (they had promised to tell me their decision last week), I called them yesterday. Turns out they were indeed pretty busy at the moment, so things got delayed, but I should expect an answer on Monday at the latest.

Just received this email (quick translation):
Dear Mr. XYZ,

thank you again for the nice interview and your interest in our company.

I'm sorry to tell you that so far, we have not been able to make a decision, as we're dependent on project management decisions.

We will keep your files, should you agree so, and contact you as soon as we're able to offer you a suitable position.

Best wishes for continued success.

So that's basically a No without saying No.
 
As I still hadn't heard from the company I applied at (they had promised to tell me their decision last week), I called them yesterday. Turns out they were indeed pretty busy at the moment, so things got delayed, but I should expect an answer on Monday at the latest.

Just received this email (quick translation):


So that's basically a No without saying No.

They are in negotiations with another candidate and you are the backup in case that falls through.
 

entremet

Member
For those in teams, how do you guys delegate bug fixes? Do you have dedicated team for it, or is rotation?

We seem to have a bottleneck between support and dev and I'm looking for best practices to speed up the process.
 

Somnid

Member
For those in teams, how do you guys delegate bug fixes? Do you have dedicated team for it, or is rotation?

We seem to have a bottleneck between support and dev and I'm looking for best practices to speed up the process.

No defined process. Usually bugs like features are queued up in order of priority by the PO. Probably harder if you have back version support or long release cycles though and you need to get bugs while promoting into the development builds. Typically the best thing is to increase the release cadence (agile) so it's not a problem and you aren't maintaining multiple things simultaneously.
 

Koren

Member
The whole zoo of features. Scala is maybe on par with Haskell when it comes to wild experimentation of language additions.

- Implicit parameters
- Implicit classes
- Call-by-name
- compound types
- "Case classes"
- Extractors
- the infamous Shapeless library
- traits
- Infix operators

Honestly, I feel very overwhelmed when I try to learn Scala!
I can easily understand that... But I work differently: I just use what I need, for now (since I'm working alone on Scala code, anyway). I look into features from time to time, keep what seems interesting, and stow away the rest.

Programmers are way to obsessed with speed. Perhaps Java frameworks are faster, but so what? Speed is rarely the issue. A site like Stack Overflow runs on c#/asp.net and i doubt most web developers work on sites that get more hits than that one.

If speed really was an issue for web sites everyone would run Erlang.
If I'm not mistaken, Youtube servers run on Python. If an interpreted language can serve dozen of millions of clients...
 

Kalnos

Banned
For people that work with development, how common is working from home?

I work from home occasionally if I'm sick, the weather sucks, etc. As long as you come into the office most of the time and you get your work done then it's cool.

I know people who have full telecommuting jobs and they aren't especially rare but they're competitive and usually require a fair amount of experience. Though I have seen companies recruiting QA engineers straight out of college.
 

HelloMeow

Member
I made a wrapper class for some arrays, so other variables don't have to be reassigned when the encapsulated object gets reassigned. It feels kinda dodgy, so my question is, is this dodgy? Is this bad practice or am I being paranoid?
 
I made a wrapper class for some arrays, so other variables don't have to be reassigned when the encapsulated object gets reassigned. It feels kinda dodgy, so my question is, is this dodgy? Is this bad practice or am I being paranoid?

What programming language? Can you post a code sample showing what you did?
 

hateradio

The Most Dangerous Yes Man
I don't get the primes one and the abba one is confusing me.

Warmup (207)
Anchors (205)
Ranges (168)
Backrefs (196)
Abba (143)
A man, a plan (169)
Prime (0)
Four (128)
Order (45)
Triples (29)
Glob (39)
Balance (180)
Powers (0)
Long count (212)
Alphabetical (0)
Powers 2 (10)

You have 1551 points.


Not sure I understand some of those. :|
 

Koren

Member
I don't get the primes one and the abba one is confusing me.
Can't say I'm surprised. I got used to regex, but I still find them half-unreadable/half-unwritable.


For the abba one, you want to exclude all words that contains {x}{y}{y}{x} where {x} and {y} are chars.

To identify {x}{y}{y}{x}, you can use (.)(.)\2\1 (a char, a second char, the second matched group, the first matched group)

To identify them inside a word, that's ^.*(.)(.)\2\1

Then you want to exclude them. For this, you need a negative look-ahead, which is (!?x) where x is the regex.

So, ^(?!.*(.)(.)\2\1)


The prime one is an interesting use of look-back.

n is not a prime if n = a*b where a and b are integers greater than 2

So you want a>1 identical groups of b>1 times the same character

b>1 times the same character is ..+ (or rather (.)\1+ in fact but here, it doesn't matter)

a>1 identical groups of b>1 times the same character is (..+)\1+

Now, you want the opposite, so throw in negative look-ahead and you get ^(?!(..+)\1+$)

Nope. It was before being acquired by Google. Now, it's a mixture of C++, Java, Go, and Python.
Still, it was working on Python no that long ago...
 

Slo

Member
Programmers are way to obsessed with speed. Perhaps Java frameworks are faster, but so what? Speed is rarely the issue. A site like Stack Overflow runs on c#/asp.net and i doubt most web developers work on sites that get more hits than that one.

If speed really was an issue for web sites everyone would run Erlang.

We're in a strange corner case when we're touting Java for it's speed and performance.
 

Kansoku

Member
I work from home occasionally if I'm sick, the weather sucks, etc. As long as you come into the office most of the time and you get your work done then it's cool.

I know people who have full telecommuting jobs and they aren't especially rare but they're competitive and usually requirement a fair amount of experience. Though I have seen companies recruiting QA engineers straight out of college.

Hmm I see, thanks.
 
Top Bottom