Thread: Bitwise Shift Operations

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by maxorator View Post
    If it needs tons of operations for stuff that x86 does with one opcode, isn't it then a very bad assembly language for learning purposes? If you'd leave the float/xmm operations out then x86 itself is a very simple assembly language. Why would someone implement an assembly language without a processor anyway?

    I am just curious.
    No, because a small set of instructions teach you more about how you do things in assembler, rather than becoming a "single operation".

    Yes, all modern processors can do multiply, all manners of shifts, and most do divide in one instruction too. But that's not necessarily going to teach you a great deal.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you need to finish an application in x86 assembly, then you won't be feeling you have too many different instructions, trust me.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by maxorator View Post
    If you need to finish an application in x86 assembly, then you won't be feeling you have too many different instructions, trust me.
    That's certainly not what I said.

    But if you have to FIRST teach a bunch of students 46 different basic instructions [each with 6-10 different addressing modes], you may spend most of the first two weeks just going through what each instruction does.

    But don't you think there really is a few too many ways of doing the same thing in x86:
    For most things, you can left-shift a number 4 bits by:
    Code:
    rol eax, 4
    
    lsl eax, 4
    
    lea eax, [eax*4]
    lea eax, [eax*4]
    
    imul eax, 16
    And those are only the most obvious different variants (and not covering MMX/SSE alternatives). The rotate/shift operations are obviously the fastest, and most obvious.

    And to the original poster: adding a number to itself should have the same result as a shift - it's a more complex operation in hardware, but aside from that, it's equivalent.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    And to the original poster: adding a number to itself should have the same result as a shift - it's a more complex operation in hardware, but aside from that, it's equivalent.
    The original poster already understood that and stated as such in the original post. The real question is about right shifting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by matsp View Post
    But don't you think there really is a few too many ways of doing the same thing in x86:
    For most things, you can left-shift a number 4 bits by:
    Code:
    rol eax, 4
    
    lsl eax, 4
    
    lea eax, [eax*4]
    lea eax, [eax*4]
    
    imul eax, 16
    Well there's an optimal way to do everything. rol is the best for normal shifting, imul is good for normal multiplication (yeah, you can multiplicate with other numbers than 1,2,4,8,16... too), lea is good for putting simple calculations to few operations and so on. Every opcode is very useful when used in the right place.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. bitwise operations with double
    By henry_kay in forum C Programming
    Replies: 2
    Last Post: 10-03-2007, 04:57 AM
  3. Bitwise operations
    By sh3rpa in forum C++ Programming
    Replies: 16
    Last Post: 09-25-2007, 06:32 PM
  4. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  5. bitwise operations
    By andrew_tucker in forum C Programming
    Replies: 2
    Last Post: 11-28-2002, 12:46 AM