Solved

Assembly

Posted on 2007-11-14
22
518 Views
Last Modified: 2010-04-15
I noticed that the c program contains if, else, and other sqrt statements that are not allowed in assembly language. How can they be modified from C to do so as in the example above.
0
Comment
Question by:dnice143
  • 11
  • 9
22 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20284467
Assembler is a very low-level (platform dependent) language. You usually don't want to use it directly. Instead you use a higher-level (platform independent) language like C, and let the compiler generate the assembler code, and the object code for you.

Of course there are no if's etc. in assembler, because those are C constructs. There are however ways of doing the same thing in assembler (obviously), using a combination of test and jump instructions.


I notice that this is your second question about converting assembler to C ... What exactly is it that you're trying to do ?
0
 

Author Comment

by:dnice143
ID: 20284493
I am trying to write a program in assembly language that solves or gives the roots for the quadratic equation only for real numbers.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20284516
Can you tell me why ? Because there's no obvious need to do that. The compiler should do a good enough job at optimizing your C code for most applications.
0
 

Author Comment

by:dnice143
ID: 20284522
I am trying to do it in assembly because it is the only method I am allowed to use. I have no clue where to begin.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20284546
Can you give a bit more background please ? What are the limitations ? What is the platform ? What's this supposed to be used for ? What is your knowledge of C ? What is your knowledge of assembler ? etc. etc.

Can you answer these questions as well as give any information you think will help us help you ? Without that information, we can't really tell you a lot ...
0
 

Author Comment

by:dnice143
ID: 20284608
In DEBUG or Assembly, write a program that would solve a quadratic equation using the quadratic formula.  The inputs are the coefficients of the equation and the output is the solutions to the equation. (your solutions are limited to those that are real and unique)

I am currently taking a microprocessor course. Last week, I created a progrm using Assembly Language that allowed a key to be entered from the keyboard, outputted the screen, and then converted to hexadecimal and binary equivalent numbers, with each dispayed as well. I am familiar with the various registers and the compare, jump, test, etc instructions.

This program will only be used to show that the task can be accomplished using an older method.
0
 

Author Comment

by:dnice143
ID: 20284635
I forgot, I am allowed to use the Debug Program as well.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20284686
ok. Thanks ... that gives me a better picture of what you want/have to do. All that is missing is which architecture you're writing assembly for ... ?? If you're not sure, can you give a short sample ?

Since you're already somewhat familiar with assembly, I guess we don't need to work on that ... So, let's get back to your original question :

>> How can they be modified from C to do so as in the example above.

The easiest answer would be to write them in C, and then let the compiler generate assembler for it. However, you would need a compiler for the specific assembler language you have to work with ... do you have one ?

Apart from that, you can of course logically derive ways of implementing C constructs in assembler yourself. Since I don't know which assembler you're using (yet), here's some "pseudo assembler" of what an if statement could look like :

             cmp reg1, reg2
             jeq label1
             ...                               /* <--- the if block */
             jmp label2
label1 : ...                               /* <--- the else block */
label2 : ...

for this C code :

             if (value1 != value2) {
                 /* if block */
             }
             else {
                 /* else block */
             }
0
 

Author Comment

by:dnice143
ID: 20284712
I am using TASM for MS-DOS Program that allows .asm files to be generated. Also, I do not have a compiler.
0
 

Author Comment

by:dnice143
ID: 20284748
It is called Tasm Borland 5.0 Software
Found:
http://vetusware.com/download/TASM%205.0/?id=65
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 20284820
I'm not familiar with TASM, but I'll assume it's x86 since it's MS-DOS.

So, my earlier example would look something like this :

             cmp bx, ax
             jnz label1
             ....
             jmp label2
label1 : ...
label2 : ...
0
 

Author Comment

by:dnice143
ID: 20284845
Yes. I belive it is .86. That really helps a lot. What about the sqrt function. How will that be used and how can I write the actaul equation in Tasm where it will not show an error?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 20284864
You'll probably want to link to the standard C sqrt function in <math.h>. As I said, I'm not familiar with TASM, so I can't tell you how to do that.

In your earlier assembler, how did you for example do output to the screen ? Can you show some sample code ?
0
 

Author Comment

by:dnice143
ID: 20284915
Sure. This is the program I wrote in Assembly where there is a main program and four procedures.
When all linked together and ran, program allows for numbers to be inputed, displayed to screen, and converted to its binary and hexadecimal equivalence.

grpmain.asm
.MODEL SMALL
.CODE
            EXTRN GRPONE:FAR
            EXTRN GRPTWO:FAR
            EXTRN GRPTHREE:FAR
            EXTRN GRPFOUR:FAR
.STARTUP
            CALL GRPONE   ;calls procedure to read 2 digits from keyboard
            CALL GRPTWO   ;converts read digits to HEX
            CALL GRPTHREE    ;converts HEX to BIN
            CALL GRPFOUR            ;displays hex and binary answers
.EXIT
END
 
Grpone.asm
.MODEL SMALL
.DATA
CONVERT                            db'CONVERT:','$'
.CODE
            PUBLIC GRPONE          
GRPONE  PROC  FAR
                    MOV DX,OFFSET CONVERT
                    MOV AH,09H                                 ;prepare DOS function call
                  INT 21H              
FRST:       MOV AH,01H             ;prepare to read from keyboard
            INT 21H                                  ;reads from keyboard
            CMP AL,30H              ;compare AL with ASCII 0                  
            JB FRST
            CMP AL,39H              ;compare AL with ASCII 9
            JA FRS
            MOV BH,AL               ;store read ASCII data to BH
SCND:       MOV AH,01H             ;prepare to read from keyboard
            INT 21H                                  ;read from keyboard
            CMP AL,30H              ;compare AL with ASCII 0
            JB SCND
            CMP AL,39H              ;compare AL with ASCII 9
            JA SCND
            MOV BL,AL               ;store read ASCII data to BL
            RET
GRPONE  ENDP
END
Grptwo.asm
.MODEL SMALL
.DATA
DEFINE          DB ?
PUBLIC DEFINE
HEX    DW ?
PUBLIC HEX
.CODE
PUBLIC GRPTWO
GRPTWO PROC FAR
            MOV AX,BX                            ;copy read data to AX
            AND AX,0F0FH                      ;mask AX
            AAD                                           ;change AX from BCD to HEX
            MOV HEX,AX                          ;copy HEX data in AX to DX
            AND AX,0F0FH                       ;mask DX
            CMP AL,0AH                           ;compare DX with 0AH
            JB NXT                                        ;jump if below to GETOUT
            MOV DEFINE,01H                 ;if above, move 1 to LETTER
NXT:        RET
GRPTWO ENDP
END
 

Grpthree.asm
.MODEL SMALL
.DATA
            EXTRN HEX:WORD
            PUBLIC SAVE
SAVE  DW ?
.CODE
            PUBLIC GRPTHREE
GRPTHREE PROC FAR
            MOV BX,HEX                        ;save original hex value to BX
            MOV BP,200H                       ;move offset of SAVE to DX
            MOV CX,08H                           ;prepare to loop 8 times
FMAST: SHL BL,1H                                            ;shift AL to the left 1 bit
            PUSH BX                                ;save BX on stack
            JC FMARST                              ;jump if carry is set to ITISI
            MOV SAVE[BP],30H            ;copy 30H into memory location of DX
            JMP REPTLM                          ;JMP to AGAIN
FMARST:   MOV SAVE[BP],31H   ;copy 31H into memory location of DX
REPTLM:   POP BX                              ;pop word from stack to BX
            INC BP                                   ;increment DX register
            LOOP FMAST                           ;loop back to NEXT
            MOV SAVE[BP],'$'                ;save end char string
            RET
GRPTHREE ENDP
END
 

Grpfour.asm
.MODEL SMALL
.DATA
            EXTRN SAVE:WORD
            EXTRN DEFINE:BYTE
            EXTRN HEX:WORD
.CODE
            PUBLIC GRPFOUR
GRPFOUR           PROC  FAR
;display
            MOV DX,HEX                        ;copy HEX to DX
            SHR DL,4H                 ;shift right DL 4 spaces
            AND DX,0F0FH                     ;Mask DX.
            ADD DX,3030H                      ;add 3030H for ASCII. 3031H
            MOV AH,02H             ;Prepare to display dl's ASCII char
            INT 21H                                  ;displays first ASCII char
            MOV DX,HEX                        ;copy original hex value back to DX
            CMP DEFINE,01H                 ;check if answer is a Ah-Fh
            JNZ DESIMA                        ;jump to label DHNUM
            AND DX,0F0FH                     ;mask DX
            ADD DX,37H              ;add 37H to make uppercase letter
            MOV AH,02H             ;prepare to show DL
            INT 21H                                  ;show DL
            JMP DESIMAS                ;jumps to binary display
DESIMA:     AND DX,0F0FH                     ;mask DX
            ADD DX,3030H                      ;change to ASCII
            INT 21H                                  ;displays second ASCII char
DESIMAS:      MOV DL,'H'                ;move ASCII "H" to DL
            INT 21H                                  ;show "H"
;display
            MOV AH,02H
            MOV DL,20H
            INT 21H
            MOV DX,AX         ;move offset of DBIN to DX
            MOV AH,09H             ;prepare to display string
            INT 21H                                  ;display string
            MOV BP,0200H                     ;move 0200H to base pointer
            MOV DX,OFFSET SAVE      ;move the offset of SAVE to DX
            ADD DX,BP                ;add the offset in DX with BP
            MOV AH,09H             ;prepare to display string
            INT 21H                                  ;display string
            RET
GRPFOUR           ENDP
END
 

0
 

Author Comment

by:dnice143
ID: 20284938
When ran,
CONVERT:  will appear on screen where you enter 2 digits to convert and it willl show equivalence
CONVERT: 99
CONVERT:99       Hex: 63H  BIN: 01100011
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 20284962
Ah, so the DOS interrupts are used directly. Ok, does TASM have a way of calling functions from compiled object code ?

You can of course also implement the sqrt function yourself. Here's a few algorithms :

        http://mathworld.wolfram.com/SquareRootAlgorithms.html
0
 

Author Comment

by:dnice143
ID: 20285048
not sure if it can but it may.
0
 

Author Comment

by:dnice143
ID: 20285248
R u familiar with MS-DOS Debug?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20287485
>> R u familiar with MS-DOS Debug?

I've never used it, no. Sorry.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20946080
I think I covered quite a bit of dnice143's questions. Since he suddenly disappeared, I can't know what more he'd want ...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now