|
Multiplications and divisions are the most time consuming operations. A programmer should always take into consideration these operations. Special instructions are developed in designing of many CPUs. In some processors these operations may take much longer time. In this case it can be considered to use another algorithm to multiply or divide by using only shit, add and subtract instructions.
Below there is an example on multiplying two numbers by shift and add instructions. The code is IBM 370 Assembly format. In this example numbers 15 and 10 are used to multiply. Result is placed into variable “RESULT”.
Grad IBM 370 Assembly v3.2 compiler and emulator from here.
IBM 370 Code
DIVSHT CSECT USING *,R15 L R2,=F'15' ; multiplier L R4,=F'10' ; multiplicand L R5,=F'0' ; load 0 into Reg5 LOOP SR R3,R3 ; erase Reg3 SRDL R2,1 ; shift right double by 1 C R3,=F'0' ; compare if Reg3 is zero BZ SKIP ; if yes don’t add to the result AR R5,R4 ; if no add to the result SKIP SLL R4,1 ; divide Reg4 by 2 C R2,=F'0' ; is multiplier zero? BNZ LOOP ; if not go to LOOP ST R5,RESULT ; store result BR R14 ; exit from this program to operating system R15 EQU 15 R14 EQU 14 R2 EQU 2 R3 EQU 3 R4 EQU 4 R5 EQU 5 RESULT DS F END
Symbol Table
Page 1
LOC ADR1 ADR2 LINE LABEL OP OPERANDS 000000 1 DIVSHT CSECT 000000 2 USING *,R15 000000 5820F040 0040 3 L R2,=F'15' 000004 5840F03C 003C 4 L R4,=F'10' 000008 5850F038 0038 5 L R5,=F'0' 00000C 1B33 6 LOOP SR R3,R3 00000E 8C200001 7 SRDL R2,1 000012 5930F038 0038 8 C R3,=F'0' 000016 4780F01C 001C 9 BZ SKIP 00001A 1A54 10 AR R5,R4 00001C 89400001 11 SKIP SLL R4,1 000020 5920F038 0038 12 C R2,=F'0' 000024 4770F00C 000C 13 BNZ LOOP 000028 5050F030 0030 14 ST R5,RESULT 00002C 07FE 15 BR R14 00002E 0000000F 16 R15 EQU 15 00002E 0000000E 17 R14 EQU 14 00002E 00000002 18 R2 EQU 2 00002E 00000003 19 R3 EQU 3 00002E 00000004 20 R4 EQU 4 00002E 00000005 21 R5 EQU 5 000030 22 RESULT DS F 000038 22 END 000038 00000000 22 F'0' 00003C 0000000A 22 F'10' 000040 0000000F 22 F'15'
Page 2
SYMBOL VALUE LENGTH TYPE ID DEF# XREF# DIVSHT 00000000 00000044 CST 01 00001 LOOP 0000000C 00000002 REL 01 00006 00013 R14 0000000E 00000001 ABS 00 00017 00015 R15 0000000F 00000001 ABS 00 00016 00002 R2 00000002 00000001 ABS 00 00018 00012 00007 00003 R3 00000003 00000001 ABS 00 00019 00008 00006 00006 R4 00000004 00000001 ABS 00 00020 00011 00010 00004 R5 00000005 00000001 ABS 00 00021 00014 00010 00005 RESULT 00000030 00000004 REL 01 00022 00014 SKIP 0000001C 00000004 REL 01 00011 00009
Page 3
LOC ADR1 ADR2 LINE LABEL OP OPERANDS LITERAL CROSS REFERENCE 000038 22 F'0' 000038 22 00012 00008 00005 00003C 22 F'10' 00003C 22 00004 000040 22 F'15' 000040 22 00003
|