Algorithm: largest of the four

  • Thread starter jackson6612
  • Start date
  • Tags
    Algorithm
In summary: C > largest){...}else if (D > largest){...}else{lntext = "largest number is ", largest}do not output anything before the summary.
  • #36
jackson6612 said:
pseudo-code
pseudo code is normally used to document an algorithm, it's an option to using a flowchart and can be editted much more easily. There are some flow charting tools that input a very "pseudo code" like langage and produce a flow chart.

What is an "unrolled loop"?
This can be done by the programmer as well as the compiler. I haven't seen many C or C++ compilers that unfold loops, or at least unfold larger loops. What I have seen is loops used to move data replaced with move string instruction sequences. Example of unfolded loop:

original code

Code:
    for(i = 0; i < n; i++){
        a[i] = b[i] + c[i];
    }

unrolled loop when n is fixed

Code:
    a[0] = b[0] + c[0];
    a[1] = b[1] + c[1];
    // ...
    a[n-1] = b[n-1] + c[n-1];

Duff's device version for when n is dynamic

Code:
    switch(n){    // max value for n is 100
      case 100:
        a[99] = b[99] + c[99];
      case 99:
        a[98] = b[98] + c[98];
      case 98:
        a[97] = b[97] + c[97];
      // ...
      case 2:
        a[1] = b[1] + c[1];
      case 1:
        a[0] = b[0] + c[0];
      case 0:
        break;
    }

As is BIOS software is part of the hardware? Does a processor have a fixed permanent memory chip which holds BIOS-like software in it for simple operations such as the difference between two numbers to be done by the processor?
A BIOS is normally read only memory at a fixed location used to boot up a computer and provide some basic I/O services. For boot up on a PC, the BIOS reads the first sector of a bootable disk and branches to the location the sector was read into (7c00 hex in memory). That code moves typically moves itself to lower memory (0600 hex in memory) and repeats the process, eventually reading sectors using BIOS calls (INT 21h) to read in a loader, such as MSDOS.SYS or NTLDR, which reads in the actual OS code and branches to that.

Some IBM mainframes use micro-code to implement a common set of instructions that are not natively available on all versions of the mainframe. Older PC's used software to emulate floating point instructions if a floating point processor was not available.
 
Last edited:
Technology news on Phys.org
  • #37
Okay, some questions. As it is obvious from your examples that there could be several ways to write an algorithm. Likewise, are there also numerous ways to write a pseudo-code? What about the language code, are there more than one way to write it? Please help me with these queries.
 
  • #38
There is not one standard form for pseudocode that I'm aware of. For a given style of pseudocode, it's very likely that two different people will come up with at least slightly differing pseudocode. When it's time to write what you're calling language code (and what programmers just call code), different people will usually come up with different implementations (code) for a given algorithm.

There is almost always more than one way to write the code that implements some algorithm. Some of these ways will be better, by some measure (code speed, code size, maintainability, etc.) and some will be worse. In many companies, code is reviewed by peers and others in an effort to improve code that needs it.
 
  • #39
I offer my thanks and best wishes to everyone who has helped me. Thank you.

Please remember that I'm neither a student of comp. science nor of science/math. So, I request you to keep your reply simple.

Before I go on to ask some follow-on questions related to the already discussed material, I would like to make new related queries. What is an 'assembler' or 'assembly language'? What is a 'high-level language', is there also some 'low-level language'? Please keep your replies simple. Thanks.
 
  • #40
jackson6612 said:
What is an 'assembler' or 'assembly language'? What is a 'high-level language', is there also some 'low-level language'? Please keep your replies simple.
Assembly language is a low-level language that is one step up from machine language, the strings of 0's and 1's that the central processing unit can work with. There are many different kinds of assembly language, one for each processor type. For example, x86 assembly language is used on the Intel family of CPUs. A completely different kind of assembly language was used on the Motorola 68x00 family of processors that were the heart of Apple Mac computers for a long time. MIPS assembly language is yet another one.

Each assembly language uses a set of instructions (op codes) to carry out a variety of operations such as moving a value from a memory location to a register in the CPU, jumping to a memory location, incrementing the value in a register or memory location, and so on. Some of the instructions used in x86 assembly are MOV, JMP, and INC.

An assembler is another kind of translater, similar to compilers and interpreters. An assembler (for a particular assembly language) translates the op codes and their operands (some instructions take one or more operands) into machine code (also called object code) that can be executed by the CPU. There is usually another step that must be taken - using a linker, which combines one or more object code files into an executable program.
 
  • #41
Assembly language at it's lowest level is machine language but using text strings to represent instruction opcodes, names for variables, and labels for instruction locations. At assembly time, the variables and labels are assigned specific addresses or offsets, and those addresses or offsets are combined with the generated instruction opcodes to create actual machine language. Some assemblers have a macro feature that is similar to a defined function in higher level languages. Some assemblers include "directives" which incorporate low level language features, such as conditionals (.if ... .elseif ... .endif).

A high level language is more human friendly and generally cpu independent, although some features may be cpu oriented, such as declaring parallel operations. Cobol is a classic high level language, with some powerful operations such as "move corresponding" which moves and formats fields of data based on input and output structure descriptions, on a common name by name basis. Fortran is another example of a high level language, but doesn't have the high end operators of Cobol. APL is also a high level language, but uses single greek symbols for it's operators and is difficult to read. Java and Python would be examples of more modern high level languages. Matlab is more of a mathematical interactive tool than a typcial high level language and includes a very large set of features.

Classic C can be considered a low level language compared to Fortran, although the main missing mathematical operator is exponentiation, which is implemented as a function (pow(...)) in C. Other common operations like input and output are also implemented as functions in a common library as opposed to being part of the native language. Mathematical expressions can be written on a single line and C will convert this into a series of machine instructions. Almost all C compilers support a common set of libraries for math, memory allocation, and I/O operations.

C++ adds enough functionality to consider it a high level language. Basic input and output operations are part of the language. The enhanced features in structures (called classes in C++) also add a lot of functionality. C++ normally includes the standard template libary, which is implemented as an include as opposed to a linked library. It allows the creation of certain types of data structures like vectors, and includes operations like sorting.

There is a step up from high level language, code generation based on user interfaces created with a tool that includes drag and drop features. The first common one I can remember was Prototyper for the Macintosh. Visual Studio also has the same feature. You "draw" the user interface by dragging components like a menu or dialog box into the intended user interface window for an application, and the tool will generate the code (operations and data) to support the interface. The user then inserts code to the generated code to complete the operations. Special comments are used in the generated code to allow the tool to distinguish generated code from programmer added code, to make updating easier. There are also some similar code and file generators for database type applications.
 
Last edited:
  • #42
Not to be mean, but most of your questions can be answered with a simple search of Google or Wikipedia. Wikipedia has a very in depth, yet approachable page on assembly.
 

Similar threads

Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
953
  • Programming and Computer Science
Replies
6
Views
973
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
769
  • Programming and Computer Science
Replies
2
Views
895
Back
Top