Sign in to follow this  

Programmer logic in everyday life

Recommended Posts

Phouchg    2,050

int GroceryShopping() {
    return Buy("Milk", g_Eggs ? 2 : 6);
}

:shiftyninja:

  • Like 1

Share this post


Link to post
Share on other sites
KRazpopov    28

local BuyMilk = 2
if Eggs == true then
BuyMilk = 6
end
print (BuyMilk)
[/CODE]

Share this post


Link to post
Share on other sites
Buttus    884

ok, someone, quick! x86 assembly!

Share this post


Link to post
Share on other sites
Radium    102

ok, someone, quick! x86 assembly!


proc BuyMilk

cmp 0, dwEggCount
jz @F ;ZF set? Jump [b]f[/b]orward
mov eax, 6 ;Has eggs, buy 6 milk.
ret

@@:
mov eax, 2 ;Has zero eggs, buy 2 milk
ret

BuyMilk endp
[/CODE]

I haven't played with assembly in a few years, but I guess that code would do if dwEggCount exists. :p

Just make sure that the amount of eggs is stored in dwEggCount and call BuyMilk to return how much milk that should be bought.

I used MASM when I played with it. A few modifications are needed to make it work for others.

I love @@-labels.

  • Like 1

Share this post


Link to post
Share on other sites
Phouchg    2,050

title groc
.model small
.stack 100h

.data
eggs dw baadh
milk dw f00dh
bag dw d00dh

.code
.486

start:
mov ax, [eggs]
cmp ax, 0
jz noeggs
mov cx, 6
jmp initbuy
noeggs:
mov cx, 2

initbuy:
xor ax, ax
buy:
add ax, 1
mov edx, [milk + ax * 4]
mov [bag + ax * 4], edx
cmp ax, cx
jne buy
endbuy:

mov ax, 4c00h
int 21h
end start

And, before anyone points out, yes. I don't know shhh about x86 assembly (contrary to, for example, the post above), but, arguably, it still looks cool and I had to pretend I'm smart :geek:

  • Like 1

Share this post


Link to post
Share on other sites
Ice_Blue    973

Yep -- posted back in June:

http://www.neowin.ne...g-a-programmer/

I just had to be a little smart alec :p

Thanks. I kind of figured it, but maybe I didn't search hard enough.

Share this post


Link to post
Share on other sites
Calum    815

Threads merged

Thanks. I kind of figured it, but maybe I didn't search hard enough.

It's hard to find such a topic when it doesn't have an obvious title (like your one did, which is why I kept this title in the merged thread) :)

  • Like 1

Share this post


Link to post
Share on other sites
Ice_Blue    973

Threads merged

It's hard to find such a topic when it doesn't have an obvious title (like your one did, which is why I kept this title in the merged thread) :)

Thanks, Calum.

Share this post


Link to post
Share on other sites
scumdogmillionaire    197

:D I like the ternary operator version as well:

int
get_milk()
{
	return ( shop_has_eggs() ) ? 6 : 1;
}

+1 that's how I'd write it :)

Share this post


Link to post
Share on other sites
+The Evil Overlord    17,448

lol

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.