5 ms·
It seems very trivial :/ I don't really do much C, but here's x86: // esi -> asciiz string // cl = character to remove // NB cl=0 won't work well ;)
by axod 16y ago
It seems very trivial :/
I don't really do much C, but here's x86:
// esi -> asciiz string
// cl = character to remove
// NB cl=0 won't work well ;)
mov edi, esi
cld
loop1:
lodsb // Load the byte from [esi] into al
cmp al, cl
je loop1 // Skip this byte
stosb
or al, al
jnz loop1
Did I miss the "gotcha"s?
edit: condensed code a bit
- JoachimSchipper 16y agoPlease don't post the solution, people would like to think for themselves.
- axod 16y agoThe worrying thing is that you're suggesting there may be people on hackernews who can't bang this out in a few seconds :/ Does it even really require much thinking? I'm not trying to be arrogant here, but it seems like a good definition of "trivial".
- jacquesm 16y ago> The worrying thing is that you're suggesting there may be people on hackernews who can't bang this out in a few seconds :/ My sentiment exactly. And by the way, you do have a bug in that code.
- axod 16y agoReally? give me a clue...?
- tsally 16y agoThe result of the program should always be a valid string. ;) Tried to be as ambiguous as I could, not really sure of another way to say it.
- axod 16y ago"valid string" ? :/ can you give me an example of an invalid string? It correctly tacks a 0 byte on the end if that's what you mean, of course cleaning up the unused space would be outside of the task at hand. Maybe I'll look like a complete idiot soon if it transpires I've completely missed the 'gotcha' ;)
- RiderOfGiraffes 16y agoI'd be really interested to see your original version and your latest version side-by-side, because I'm wondering if your "clean-up" actually changed its behavior.
- tsally 16y agoActually, I might just be tired. :-p I don't see anything wrong with axod's code. Just emailed you a fairly verbose C version. I hate writing production code...
- axod 16y agoOriginal code: // esi -> asciiz string // cl = character to remove mov edi, esi cld loop1: lodsb // Load the byte from [esi] into al or al, al jz alldone cmp al, cl je loop1 // Skip this byte, since it's one we want to remove stosb // Store the byte at [edi] from al jmp loop1 // Loop around for the next byte alldone: xor al, al stosb // 0 terminate it The only real behavioral change was that this version would cope with an input of cl=0. My cleaned up version would not cope with cl=0, but would need a sanity check for that.
- RiderOfGiraffes 16y agoHmm. My memory must be faulty, but I don't remember the first one having the final xor and stosb. I do remember looking at your solution and thinking that you'd forgotten it, that's why I expressed an interest in seeing the original. Still, likely my memory is at fault.
- jacquesm 16y agoYou are making some scary assumptions about the validity of that pointer...
- axod 16y agoI didn't include all the boring error checking code, I included just the 'meat' ;) If you call it with valid inputs, it produces valid outputs. IMHO That's not a 'bug'. That's a lack of boring gruntwork error checking.
- jacquesm 16y agoYes, that's more or less the problem with lots of code. I'm not saying your code is invalid or that it won't work, but 'defensive' programming says: - assume your input is total garbage - handle all correct situations correctly - handle all garbage gracefully or throw an error depending on what the circumstances dictate I'm also missing the stackframe handling, but as you said, this is the 'meat', that meat needs a bit of scaffolding to work.
- axod 16y agoOh definitely. I agree. I assumed though that the "problem" itself was the main aim here, not testing if someone can write scaffolding error checking code. How far do you go? Check that the memory isn't mapped as read only? ;) Check that there's actually some memory mapped into that address space? Obviously you need both skills, but they're pretty related I'd say. If you can solve low level problems you can probably think through any potential issues, invalid inputs, edge cases etc and churn out code to deal with them. I guess my issue is I don't see what being able to solve this says about someone, apart from "isn't completely terrible at programming".
- NickPollard 16y agoDefensive programming is not always the correct assumption though.
- 16y ago
- j_baker 16y agoIt's not that it requires much thinking. It's just that it's not every day that most of us have to code something like this in C.
- JoachimSchipper 16y agoFrom another I made on this page: > If you don't find any egregious bugs, I hereby claim it really is trivial. Still, it may be a nice exercise.
- deleted 16y ago[deleted]
- jemfinch 16y agoHow many HNers do you think know x86 assembly so well that simply seeing the structure of the code gives away the answer? :)
- jacquesm 16y agoEven if you didn't know x86 at all you should still be able to figure out what it does by looking at it given the fact that the specification of what it does is only one sentence long.
- jemfinch 16y agoNo doubt, but if my desire is not to see other answers, I'm not going to read the assembly closely enough to understand. Your objection makes more sense with indented programming languages whose indentation structure may give away more information.
- axod 16y agoIt's 8 instructions. It's a single loop. It's moving stuff from one buffer to another and ignoring specific bytes. What are you talking about? What's to understand?
- jemfinch 16y agoI can ignore x86 assembly just like I can ignore French: the graphical structure (i.e., indentation) of an x86 program gives away nothing, just like a paragraph of French gives me no immediate sense of its meaning. In either case, I could probably puzzle out the meaning with intense reading, but the mere presence of the text does not give it away. My C solution, on the other hand, has a distinct graphical structure which would indicate, even to people for whom C isn't their "native language," a lot of the meaning of the text with merely a glance.
- xenophanes 16y agoAre you saying that some people couldn't do the problem, but could actually read that Assembly?
- barrkel 16y agoI think it's trickier than FizzBuzz, but not by a whole lot.
- jacquesm 16y agoI think it is actually simpler. And the fizz-buzz problem has two structurally different solutions.
- jonsen 16y agoedit: condensed code a bit Well well, the other version seemed to work ;-)
- Agent101 16y agoNot much experienced with x86 asm, but you don't seem to be incrementing esi anywhere... What am I missing?
- jacquesm 16y agostosb = store sequential byte increments the destination pointer, just like lodsb increments the source pointer.
- Agent101 16y agoAh, thanks. Also, eww. It decrements or increments dependent upon a flag. Not nice.
- jacquesm 16y agox86 assembly is full of old cruft, that 'cld' there is a thing that if you've ever forgotten it probably cost you a lot of time. 99% of the time the flag is clear... unless it isn't. So for a seasoned assembly programmer that cld is idiomatic, axod probably typed the instruction reflexively because he knows he can't rely on the state of the direction flag, even if it has nothing to do with the problem per-se.