Thread: Mixed Language: C and Asm

  1. #16
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Because if i had
    Z 90 = 0b 0101 1010
    z 122 = 0b 0010 0000

    0b 0101 10101 | 0x20 would give me something else, not 122.
    EDIT:
    i think i made mistake in the calculation. brb
    You ended that sentence with a preposition...Bastard!

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    90 is 0b01011010, true. 122, however, is 0b01111010, not 0b00100000 which is just 32.

  3. #18
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Yeah it worked.
    I am going to try the reverse, convert from lower to upper. I don't understand why we AND, and not OR.
    You ended that sentence with a preposition...Bastard!

  4. #19
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    I just did it. And I just had to do
    a&~0x20...where did 0xdf come from, it works but why not use ~0x20? :S
    You ended that sentence with a preposition...Bastard!

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    I just did it. And I just had to do
    a&~0x20...where did 0xdf come from, it works but why not use ~0x20? :S
    If you can spot the difference between ~0x20 and 0xdf, then you're a better man than I Gunga Din.

    As to why the difference between and and or, think about what needs to happen for five seconds.

  6. #21
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    ~0x20 is a negative number -224
    0xdf is a positive 223.
    btw, who is Gunga Din?

    AND is used to clear, while OR was used to set? (all this binary...)
    You ended that sentence with a preposition...Bastard!

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    ~0x20 is a negative number -224
    0xdf is a positive 223.
    Um, no. ~0x20 = 0b 1101 1111, while 0xdf is 0b 1101 1111. (However, if I'm not mistaken, you can type 0xdf in an assembler program, while you can't type ~0x20. I might be wrong there though.)
    Quote Originally Posted by Eman View Post
    btw, who is Gunga Din?
    Gunga Din - Wikipedia, the free encyclopedia

    Quote Originally Posted by Eman View Post
    AND is used to clear, while OR was used to set? (all this binary...)
    So, you do know what and means, right? And you do know what or means, right?

  8. #23
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    Um, no. ~0x20 = 0b 1101 1111, while 0xdf is 0b 1101 1111. (However, if I'm not mistaken, you can type 0xdf in an assembler program, while you can't type ~0x20. I might be wrong there though.)
    i should really use a calculator.

    So, you do know what and means, right? And you do know what or means, right?
    em, what i have said? lol i don't know what you want me to see
    You ended that sentence with a preposition...Bastard!

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    i should really use a calculator.


    em, what i have said? lol i don't know what you want me to see
    "and" means "both have to be true (or 1)". "or" means "only one has to be true (or 1)". So you should be able, at some point, to see the difference between 0b 0101 1001 AND 0b 0010 0000, and 0b 0101 1001 OR 0b 0010 0000. And the same for working with 0b 1101 1111.

  10. #25
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    "and" means "both have to be true (or 1)". "or" means "only one has to be true (or 1)". So you should be able, at some point, to see the difference between 0b 0101 1001 AND 0b 0010 0000, and 0b 0101 1001 OR 0b 0010 0000. And the same for working with 0b 1101 1111.
    Code:
      0101 1001
    &0010 0000
      0000  0000==0
    
      0101 1001
    | 0010 0000
      0111 1001==121     
    
       0101 1001 
       1101 1111
    & 0101 1001==89  gives me original number
    
       0101 1001
    |  1101 1111
       1101 1111==0xdf gives me the mask
    There is a difference alright. But what are you trying to tell me?
    You ended that sentence with a preposition...Bastard!

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Eman View Post
    Code:
      0101 1001
    &0010 0000
      0000  0000==0
    
      0101 1001
    | 0010 0000
      0111 1001==121     
    
       0101 1001 
       1101 1111
    & 0101 1001==89  gives me original number
    
       0101 1001
    |  1101 1111
       1101 1111==0xdf gives me the mask
    There is a difference alright. But what are you trying to tell me?
    You asked why we use and in some circumstances, and or in others. I am attempting (and apparently failing) to show you the difference between them so that you know why we use and sometimes, and or other times.

  12. #27
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    tbh from that. All I can say is you could use the bitwise & to check if every bit is 0. Or to retain the original bits.
    And the OR is used to set individual bits.

    I wrote this code, and i have been getting seg fault. Can you help me spot what is wrong.I have been at it for half an hr. Ty
    Code:
    
    #include <stdio.h> 
    
    void cstring(char *s, char *d) ;
    int main()
    {
      char source[]="Hello World" ;
    
      char dest[12] ;
    
      printf("%s\n", source) ;
    
      cstring(source, dest) ;
    
      //printf("After function call: \n") ;
    //  printf("%s\n", dest) ;
     
    
      
      //fputs(source, stdout) ;
    
      return 0 ;
    }
    Code:
    .section .data
    
    .section .text
    
    	.global cstring
    
    cstring:
    	push %ebp
    	mov %esp, %ebp
    
    	push %esi
    	push %edi
    	push %eax 
    	mov 12(%ebp),%esi  #esi to point to source
    
    	mov 8(%ebp),%edi   # edi to point to dest
    
    copyLoop:
    	cmpb $0, (%esi)
    	je exit
    
    	movb (%esi),%cl
    	movb %cl, (%edi)
    
    	inc %esi
    	inc %edi
    
    	jmp copyLoop
     
    
    exit:
    	mov %ebp, %esp
    	pop %eax
    	pop %edi
    	pop %esi
    	pop %ebp
    
    	ret
    	.end
    You ended that sentence with a preposition...Bastard!

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since arguments are pushed right-to-left, the one on the "top" of the stack (nearest ebp) is the first argument.

  14. #29
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    so it should be
    movl 8(%ebp),%esi to point to source.
    I tried that, i still got the same error
    You ended that sentence with a preposition...Bastard!

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also:
    Code:
    mov %ebp, %esp
    means "SAY GOODBYE TO THE STACK!!!!!" The whole purpose of the pops is to bring esp down while moving the clobbered registers back to what they're supposed to be. If you fiddle with esp, now you're popping the old ebp into eax, the eip (i.e., where to return to from this function) into edi, probably the [11] byte of one of your strings (depending which one was loaded where) into esi, the [10] byte of that string into ebp [eep!] and then using the [9] byte of the string as the return address to jump to with ret. And that's not going to be pleasant.

Popular pages Recent additions subscribe to a feed